I have a class with the following property. It constructs a SelectList
object from an existing list, and then sets the selected item.
public SelectList ProviderTypeList
{
get
{
SelectList list = new SelectList([...my collection...], "Value", "Key");
SelectListItem item = list.FirstOrDefault(sli => sli.Text == SelectedProviderType);
if (item != null)
item.Selected = true;
return list;
}
}
However, when this code is finished, item.Selected
is true. But the corresponding item in the SelectList
collection is still null.
I can't seem to find a way to update the object in the collection, so that the setting will be used in the resulting HTML.
I'm using @Html.DropDownListFor
to render the HTML. But I can see that the object within the collection was not modified as soon as this code has executed.
Can anyone see what I'm missing?
SelectedValues
property on the list instead of settingSelected
on the item? – CaitlyncaitrinSelectedValues
is read-only and cannot be written to. – Reta