C# 使用 DropDownListFor 添加“必需”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18704902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:58:06 来源:igfitidea点击:
Adding the 'required' attribute with DropDownListFor
提问by idlackage
I have this:
我有这个:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"))
And would like it to be rendered as this:
并希望将其呈现为:
<select required>
<option>...</option>
...
How would I do this?
我该怎么做?
采纳答案by Yuriy Faktorovich
Use this:
用这个:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = "required"})
It won't achieve the shorthand <select required
but it should have the same effect. Although I've found you could achieve that exact element using
它不会实现速记,<select required
但应该具有相同的效果。虽然我发现你可以使用
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = (string)null})
Which is a little ugly.
这有点难看。
回答by JoshYates1980
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"})
回答by Manoj Tripathi
Try the following
尝试以下
@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })