Reputation: 1844
The following block of code is used to make the selected value of a dropdownlistfor persist after submit:
@Html.DropDownListFor(x => Model.Value, new SelectList(Model.Values, @Model.Value))
In this block of code, Model.Values
is a list of strings and the @Model.Value
specifies which value is selected.
This block of code works great but I'm trying to do the same thing with a list of SelectListItem
s (rather than a list of strings). In the following code Model.Values
is a list of SelectListItem
s.
@Html.DropDownListFor(x => Model.Value, Model.Values, Model.Value,
new
{
id = "ValueDD", onchange = "this.form.submit()"
})
Is it possible to use @Model.Value
as I did in the first code block to set the selected value?
Upvotes: 0
Views: 436
Reputation: 2530
SelectListItem
has a property Selected
that needs to be set for the selected item, see http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.ASPX
So in your controler you can just set the Selected
property of the item that needs to be selected, assuming you are using the controler to build the list of Model.Values
Upvotes: 1