假设我有一个枚举:
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 框架会自动添加“已检查”属性?谢谢。
该模型:
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>