bilpor
bilpor

Reputation: 3889

setting default item on dropdownlistfor

I have populated a selectlist in my viewmodel in the form:

 DeliveryCentres = new SelectList(deliveryCentres, "Id", "CentreName");

in my View I have:

 @Html.DropDownListFor(m => m.DeliveryCentreId, Model.DeliveryCentres, "-- Not Required --");

The problem is that on display the dropdown defaults to the first item in the list as opposed to the -- Not Required -- value.

How do I make this the default?

Upvotes: 1

Views: 52

Answers (1)

bilpor
bilpor

Reputation: 3889

@Stephen Muecke has answered the question. I just needed to leave the ID as null in the ViewModel before rendering.

From comments:

The selected value will be whatever the value of DeliveryCentreId is. If its value is null or does not match one of the option values, then the "Not Required" option will be selected

Upvotes: 2

Related Questions