andtodd
andtodd

Reputation: 240

Set value for SelectList option label

I have a Drop Down List in my MVC application, which is populated from my "PropertyList" model.

I have set the propertyList variable as follows to obtain the details needed for each option as follows;

var propertyList = new SelectList(Model.propertyList, "fullPropertyDetail", "FullAddress");

@Html.DropDownList("addresslist", (SelectList)propertyList, "-- Please select an address from the list below --", new { @id = "valid-addresslist" })

This populates the list as expected and the default option reads "-- Please select an address from the list --", however the value for that option is set as "".

Is there any way I can set the default option value as "none" as another system looks for this value if it is selected.

Upvotes: 0

Views: 1615

Answers (2)

TanvirArjel
TanvirArjel

Reputation: 32069

Simply you can do as follows:

@{
   List<SelectListItem> selectListItems = propertyList.ToList();
   selectListItems.Insert(0, (new SelectListItem { Text = "Please select", Value = "none", Selected = true }));
}
@Html.DropDownList("addresslist", selectListItems, new { @id = "valid-addresslist" })

Upvotes: 2

Mike Bovenlander
Mike Bovenlander

Reputation: 5426

Add the "-- Please select ..... below --"option as SelectListItem in the propertyList List.

Like: new SelectListItem { Text = "-- Please select ..... below --", Value = "none"}

Upvotes: 0

Related Questions