Search code examples
c#html.netrazordropdownlistfor

How to set new attribute to a @Html.DropDownListFor


I'm trying to add an specific attribute (data-toggle="select") to my DropDownListFor.

When I use:

@Html.DropDownListFor(model, myList, "Select..", new { @class = "form-control", @data-toggle="select" }). 

I get the error saying that "Cannot resolve symbol 'data'" and "Cannot resolve symbol 'toggle'"

What can I do so my DropDownListFor recognizes the attribute data-toggle?

Ps: When I use

@Html.DropDownListFor(model, myList, "Select..", new { @class = "form-control"})

it works just fine


Solution

  • The answer is actually pretty simple, just replace your -(dash) with a _(underscore) and you will be set.

    @Html.DropDownListFor(model, myList, "Select..", new { @class = "form-control", @data_toggle="select" }).