MARS
MARS

Reputation: 587

How to add and empty select list with Html.DropDownList

I am trying to add a drop down select element to my .net page, but I need it to be black so I can populat it with a json list I have with jQuery. Right now, my view has this:

@Html.DropDownList("Language", new SelectList(" "), "name = 'Laguange'", new { @class = "Language-List field form-group form-control" })

This 'works', but it seems to be confusing some stuff when I try to submit the form it is in. The value I select from it is never seen by the model, as it keeps triggering my [Required] validation helper.

Does anyone know How to add this without the new SelectList(" ") attribute?

Upvotes: 0

Views: 70

Answers (1)

Glynn Hurrell
Glynn Hurrell

Reputation: 652

I would suggest it's because youre using the name attribute in the wrong place. Try this:

 @Html.DropDownList("Language", new SelectList(" "), new {Name="Language", @class = "Language-List field form-group form-control" })

Notice the capital Name

Although as they are the same, you can just leave it out:

 @Html.DropDownList("Language", new SelectList(" "), new {@class = "Language-List field form-group form-control" })

Upvotes: 1

Related Questions