假设我有一个枚举:
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"
为什么MVC框架会自动添加"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, OrderStatusType.Shipped, new {@checked = true})
<span>@OrderStatusType.Shipped</span>
@Html.RadioButtonFor(m => m.Status, OrderStatusType.Waiting)
<span>@OrderStatusType.Waiting</span>