说我有一个枚举:
public enum OrderStatusType { Waiting = 0,Pending,Picked,Shipped,}
我生成单选按钮列表如下.
@Html.RadioButtonFor(m => m.Status,OrderStatusType.Shipped,new {@checked = true}) @Html.RadioButtonFor(m => m.Status,OrderStatusType.Waiting)
但等待总是被选中.输出HTML如下:
input type="radio" value="Shipped" name="Status" id="Status" checked="True" input type="radio" value="Waiting" name="Status" id="Status" checked="checked"
该模型:
public class Orderviewmodel { public OrderStatusType Status { get; set; } }
风景:
@using WebApplication17.Controllers @model WebApplication17.Models.Orderviewmodel @{ ViewBag.Title = "Home Page"; } @Html.RadioButtonFor(m => m.Status,OrderStatusType.Picked) <span>@OrderStatusType.Picked</span> @Html.RadioButtonFor(m=>m.Status,OrderStatusType.Pending) <span>@OrderStatusType.Pending</span> @Html.RadioButtonFor(m => m.Status,new {@checked = true}) <span>@OrderStatusType.Shipped</span> @Html.RadioButtonFor(m => m.Status,OrderStatusType.Waiting) <span>@OrderStatusType.Waiting</span>
解决方法
它的选择是因为您的属性状态的值是OrderStausType.Waiting(这是绑定如何工作!).
从@ Html.RadioButtonFor()方法和控制器中删除新的{@checked = true},将状态值设置为OrderStausType.Shipped,然后再将其传递给视图.
还请注意,您应该使用< label>元素而不是< span>将标签与单选按钮相关联
@Html.RadioButtonFor(m => m.Status,OrderStausType.Picked,new {id = OrderStausType.Picked) <label for=@OrderStausType.Picked>@OrderStausType.Picked</label>