Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加';必需的';具有DropDownListFor的属性_C#_Razor - Fatal编程技术网

C# 添加';必需的';具有DropDownListFor的属性

C# 添加';必需的';具有DropDownListFor的属性,c#,razor,C#,Razor,我有这个: @Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text")) 并希望将其呈现为: <select required> <option>...</option> ... ... ... 我该怎么做?使用以下方法: @Html.DropDownListFor(x => x.SelectedVal

我有这个:

@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"})
它无法实现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"})
试试下面的方法

@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })

您是否将模型上的值设置为
[Required]
?另请注意,根据w3c,它还不受支持。@RômuloSpier
[Required]
显然不是有效的关键字。我是否必须使用
语句或其他语句放置任何
,才能使其工作?@idlackage,是的,您应该使用System.ComponentModel.DataAnnotations添加
new{required=String.Empty}
可能也会这样做-我正要尝试:)
new{required=default(String)}
生成速记
@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })