使用@html.RadioButtonFor MVC4 ASP.Net绑定4个单选按钮

Binding 4 Radio buttons using @html.RadioButtonFor MVC4 ASP.Net

这是我的枚举:

public enum ValidityTypes : int
{
    [Description("Not Set")]
    None = 0,
    [Description("Zero Validity")]
    ZeroValidity = 1,
    [Description("Life Time Validity")]
    LifeTimeValidity = 2,
    [Description("N Months")]
    NMonths = 3,
    [Description("Rehire Validity in Months")]
    RehireValidity = 4
}

这里我设置的模型属性:

public ValidityTypes ValidityType { get; set; }

这是我的看法:

<label><b>Zero Validity:</b></label>
@if (Model.ValidityType == Constant.ValidityTypes.ZeroValidity) 
{
    @Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" })             
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" })             
}
<label><b>Life Time Validity: </b></label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.LifeTimeValidity) 
{
    @Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" }) 
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" }) 
}
<label><b>'N' Months:</label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.NMonths) 
{ 
    @Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "true", onClick = "Validity3EnableDisable(this);" ,  autocomplete="off" }) 
    @Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px" })
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "false" , onClick = "Validity3EnableDisable(this);" ,  autocomplete="off"})
    @Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px", disabled = "true" })
}

现在,当我加载此视图时,我得到 model.Validitytype 作为 ZeroValidity,这与数据库中的相同,并且满足第一个 if 条件。但是在完整的视图加载中,N months 单选按钮即将被选中。

为什么我无法显示正确选择的单选按钮? 难道是我的@checked属性的控件没用了?

checked="true"checked="false"checked="anythingAtAll" 都表示同一件事 - 单选按钮将被选中。 checked 属性是一个 boolean 属性,这意味着它的存在决定了它是否被选中。在您的情况下,始终会检查最后一个,因为这是最后一个呈现的属性(该属性已从之前的属性中删除,因为只允许检查一个)。

你永远不应该设置 checked 属性,因为 RadioButtonFor() 方法是根据你绑定到的模型的值来设置的(模型绑定就是这样工作的)。删除所有 if/else 块并将代码更改为

<label>
    @Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.ZeroValidity, new { id = "", @class = "validity" })
    <span>Zero Validity</span>
</label>
<label>
    @Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.LifeTimeValidity, new { id = "", @class = "validity" })
    <span>Life Time Validity</span>
</label>
// labels below omitted for brevity
@Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.NMonths, new { id = "", @class = "validity" })
@Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.RehireValidity, new { id = "", @class = "validity" })

然后使用 css 来设置元素的样式而不是内联样式,例如

.validity {
    width: 50px;
}

并使用 Unobtrusive JavaScript 来处理您的事件

$('.validity').click(function() {
    ....
});