14

我有这个:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"))

并希望它呈现为这样:

<select required>
    <option>...</option>
    ...

我该怎么做?

4

3 回答 3

30

用这个:

@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})

这有点难看。

于 2013-09-09T19:05:38.673 回答
0

尝试以下

@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })
于 2017-12-29T20:03:42.670 回答
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"})
于 2016-10-26T14:41:22.453 回答