2012-06-04 44 views
0

单一模式preoperty你怎么绑定使用@Html.RadioButtonFor()绑定单选按钮,在MVC3

+1

可能的重复http://stackoverflow.com/questions/5768274/correct-way-to-bind-an-mvc3-radiobutton-to-a-model – pollirrata

+0

阅读第一个答案..我仍然不明白如何达到正确的价值模型属性.. –

+1

它们应该具有相同的名称。如果您编写radiobuttonfor,那么您应该为两个单选按钮传递参数htmlattribute new {name =“a”}。最好使用单选按钮而不是单选按钮 – karaxuna

回答

0

在你的模型在MVC模式单一属性2个单选按钮:

public class MyModel 
{ 
    public bool? DoYouAgree { get; set; } 
} 

在你看来:

<label> 
    @Html.RadioButtonFor(m => m.DoYouAgree, false, 
     new { id = "DoYouAgree_false" }) 
    No 
</label> 
<label> 
    @Html.RadioButtonFor(m => m.DoYouAgree, true, 
     new { id = "DoYouAgree_true" }) 
    Yes 
</label>