C# 添加';必需的';具有DropDownListFor的属性
我有这个:C# 添加';必需的';具有DropDownListFor的属性,c#,razor,C#,Razor,我有这个: @Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text")) 并希望将其呈现为: <select required> <option>...</option> ... ... ... 我该怎么做?使用以下方法: @Html.DropDownListFor(x => x.SelectedVal
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"))
并希望将其呈现为:
<select required>
<option>...</option>
...
...
...
我该怎么做?使用以下方法:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = "required"})
它无法实现VB.NET的速记
@Html.DropDownListFor(Function(Model) Model.ProgramTypeId, New SelectList(Model.allPrograms,
"ProgramTypeId", "ProgramName"), "Select Program....",
New With {Key .class = "form-control", Key .required = "Required"})
试试下面的方法
@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })
您是否将模型上的值设置为[Required]
?另请注意,根据w3c,它还不受支持。@RômuloSpier[Required]
显然不是有效的关键字。我是否必须使用
语句或其他语句放置任何,才能使其工作?@idlackage,是的,您应该使用System.ComponentModel.DataAnnotations添加
new{required=String.Empty}
可能也会这样做-我正要尝试:)new{required=default(String)}
生成速记
@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })