c# - 使用 DropDownListFor 添加 'required' 属性

标签 c# razor

我有这个:

@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/

相关文章:

c# - 如何在 C# 中以原子方式交换 2 个整数?

c# - 如何以编程方式将服务依赖项添加到已安装的 Window 服务?

c# - ASP.NET MVC $.post 调用返回字符串...需要有关 jqGrid 格式的帮助

asp.net - Html.Raw 为空

c# - 将特殊字符分配给字符串时,Razor 页面会弄乱编码

css - 为什么一个div比另一个div高?

c# - 从颜色中去除透明度

jquery - 无法缩进表中的 th 和 td 元素

c# - 将模型属性作为参数传递给 Url.Action

c# - 错误 : "The SqlParameter is already contained by another SqlParameterCollection" when adding a Parameter