5

我有一个模型

 public class SexModel
 {
    public SexModel()
    {

        this.Man = "Man";
        this.Woman = "Woman";
        this.ManId = 1;
        this.WomanId = 2; 
        this.WomanSelected = this.ManSelected = false;
    }

    public bool ManSelected { get; set; }
    public bool WomanSelected { get; set; }
    public string Man { get; set; }
    public string Woman { get; set; }
    public int ManId { get; set; }
    public int WomanId { get; set; }

 }

在我的视图上创建一个单选按钮

 @Html.RadioButton(Model.Man,  Model.ManId, Model.ManIsSelected, 
                   new { @id = Model.ManId})


 @Html.RadioButton(Model.Man, Model.WomanId, Model.WomanSelected,
                   new { @id = Model.WomanId })

用户可以在注册表单上选择男性或女性单选按钮,但为什么在我的操作中单击提交表单按钮后,WomanSelected 和 ManSelected 总是错误的?

4

1 回答 1

5

您应该通过RadioButtonFor绑定 MVC 中的单选按钮,即

@Html.RadioButtonFor(m => m.ManSelected, m.Man);
@Html.RadioButtonFor(m => m.WomanSelected, m.Woman);
于 2013-06-30T11:16:01.203 回答