MVC 5基于模型检查RadioButtonFor

时间:2015-08-08 18:06:29

标签: asp.net-mvc asp.net-mvc-5

我遇到了一个我无法找到解决方案的问题。

这样的检查按钮有效:

@Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 
    ? new { id = "B", @checked = "checked" } : null)

但我还需要设置id,我该怎么做?这段代码:

@Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 
    ? new { id = "B", @checked = "checked" } : new { id = "B" })

给出错误:

  

无法确定条件表达式的类型,因为'AnonymousType#1'和'AnonymousType#2'之间没有隐式转换

这超出了我的理解范围。

我想应该有一个微不足道的解决方案吗?

2 个答案:

答案 0 :(得分:0)

将它们投放到object

Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 
    ? (object)new { id = "B", @checked = "checked" } : (object)new { id = "B" });

答案 1 :(得分:0)

选中link

@{string id = ViewData.TemplateInfo.GetFullHtmlFieldId("radioTrue");}
@Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 ? new { id } : null)

这方面的一个很好的例子是here

相关问题