我有这个:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"))
并希望将其呈现为:
<select required>
<option>...</option>
...
我该怎么做?
答案 0 :(得分:23)
使用此:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = "required"})
它不会达到速记<select required
但它应该具有相同的效果。虽然我发现你可以使用
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = (string)null})
哪个有点难看。
答案 1 :(得分:0)
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"})
答案 2 :(得分:0)
尝试以下
@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })