如何在 Razor Html.DropDownList 上设置标签值

Beo*_*lve 0 html razor

我想设置下拉列表的标签值(不是默认值,标签值),我认为我做错了什么

@Html.DropDownList("cboCategoria", new SelectList(Model, "ID", "Nome"), new { @id = "cboCategoria", @label = "Categoria-pai: " })
Run Code Online (Sandbox Code Playgroud)

Dan*_*ses 6

您可以通过几种方式来做到这一点,标签与实际标签的创建是分开的<select>

<label>Categoria-pai: @Html.DropDownList(...)</label>
Run Code Online (Sandbox Code Playgroud)

或者

<label for="cboCategoria">Categoria-pai:</label> @Html.DropDownList(...)
Run Code Online (Sandbox Code Playgroud)

或者

@* This assumes you are creating the dropdown from a property named 
   cboCategoria in your Model *@
@Html.LabelFor(m => m.cboCategoria) @Html.DropDownList(...)
Run Code Online (Sandbox Code Playgroud)

编辑:我确实想指出,如果您使用最后一种方法,您将需要模型属性上的 [Display] 属性。