Keith
Keith

Reputation: 361

Specify DropDownList options in MVC 3 Razor

I'd like to know if it is possible to assign a default value for a Dropdownlist while restricting User to select that default value from its option list.

Example :

@Html.DropDownListFor(m => m.Something, new SelectList(Model.TheList), "--DefaultValue--", new { name="SomethingName", id="SomethingID", style="width:150px;"})

Let's suppose TheList contains {"1","2","3"}. When User clicks the DropDownList, he will find "--DefaultValue--" displayed as initial value, and also find it as one of the options available to be selected.

Is it possible only to have 1,2, and 3 alone as the options without "--DefaultValue--" ?

Upvotes: 0

Views: 494

Answers (2)

user2290070
user2290070

Reputation: 27

Try this:

@Html.DropDownListFor(m => m.Something, new SelectList(Model.TheList),new { name="SomethingName", id="SomethingID", style="width:150px;"})

Upvotes: 0

Piotr Stapp
Piotr Stapp

Reputation: 19838

The only way is to set selected value in DropDown. --Default value-- represents null in selected value

Upvotes: 0

Related Questions