Beoulve
Beoulve

Reputation: 57

How to set label value on Razor Html.DropDownList

i want to set the label value for a dropdownlist(not the default value, the label value) and i think im doing something wrong

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

Upvotes: 0

Views: 10253

Answers (1)

Daniel Moses
Daniel Moses

Reputation: 5858

You can do it a few ways, The label is seperate from the creation of the actual <select>:

<label>Categoria-pai: @Html.DropDownList(...)</label>

OR

<label for="cboCategoria">Categoria-pai:</label> @Html.DropDownList(...)

OR

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

EDIT: I did want to note, that if you use the last method, you will want a [Display] attribute on your Model's property.

Upvotes: 6

Related Questions