RadioButtonFor用法

时间:2014-01-29 14:51:37

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview

这是我的代码:

_testPartial.cshtml(部分视图)

 @Html.RadioButtonFor(model => model.CardType, "G", new { @checked = @Model.IsGold})
 @Html.RadioButtonFor(model => model.CardType, "P", new { @checked = @Model.IsPlatinum})
 @Html.RadioButtonFor(model => model.CardType, "B", new { @checked = @Model.IsBusiness})

控制器

public PartialViewResult ShowCardtype() 
{ 
    Model cardType= new Model {
    IsGold=true,
    IsPlatinum=false,
    IsBusiness=false};

    return PartialView("_testPartial",cardType);
}

预期的行为是,应该在表单中选择GOLD单选按钮选项。但它总是选择最后一个选项。我试过返回“已检查”的字符串,但没有运气。感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

第二个选项是选中的选项,您不需要新的部分。

@Html.RadioButtonFor(model => model.CardType, "G") 
@Html.RadioButtonFor(model => model.CardType, "P") 
@Html.RadioButtonFor(model => model.CardType, "B")

将视图集model.CardType加载到G,P或B时,它将选择正确的单选按钮

相关问题