我有这个:
@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"})
它不会实现简写<select required
但它应该具有相同的效果。尽管我发现您可以使用
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = (string)null})
有点丑。
关于c# - 使用 DropDownListFor 添加 'required' 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18704902/