我的视图模型定义了必须显示为组合框的属性.属性定义是:
[Required]
public int Processor { get; set; }
Run Code Online (Sandbox Code Playgroud)
我正在使用DropDownListFor渲染组合框:
<%=Html.DropDownListFor(r => r.Processor, Model.Processors, Model.Processor)%>
Run Code Online (Sandbox Code Playgroud)
Model.Processors包含IEnumerable<SelectListItem>一个特殊项目定义为:
var noSelection = new SelectListItem
{
Text = String.Empty,
Value = "0"
};
Run Code Online (Sandbox Code Playgroud)
现在我需要在我的组合框中添加验证,以便用户必须选择不同的值,然后选择'noSelection'.我希望有一些配置,RequiredAttribute但它没有默认值设置.
我正在研究ASP.NET MVC-4 Web应用程序.我在我的action方法中定义了以下内容来构建一个SelectList:
ViewBag.CustomerID = new SelectList(db.CustomerSyncs, "CustomerID", "Name");
Run Code Online (Sandbox Code Playgroud)
然后我在我的DropDownListFor内部渲染我View:
@Html.DropDownListFor(model => model.CustomerID, (SelectList)ViewBag.CustomerID, "please select")
Run Code Online (Sandbox Code Playgroud)
如图所示,我将ViewBag属性命名为等于Model属性名称CustomerID.从我自己的测试中,定义相同的名称不会导致任何问题或冲突,但我应该避免这种情况吗?
asp.net asp.net-mvc html-helper html.dropdownlistfor asp.net-mvc-4