5

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?

5
  • Have you tried setting the SelectedValues property on the list instead of setting Selected on the item? May 16, 2013 at 22:26
  • I just tried to figure that out, but I get the error that SelectedValues is read-only and cannot be written to. May 16, 2013 at 22:32
  • I see. Maybe this answer can help you? stackoverflow.com/questions/301854/… May 16, 2013 at 22:37
  • please add your dropdown creation code May 16, 2013 at 22:37
  • @DaveA: My question is not about the dropdown creation code. As I stated, I can see in the debugger that it is not set. May 16, 2013 at 22:40

3 Answers 3

15

There is an optional additional parameter in SelectList

SelectList list = new SelectList([...my collection...], "Value", "Key", SelectedID);

Check the definition

public SelectList(IEnumerable items, string dataValueField, string dataTextField, 
object selectedValue);

which sets the selected value and is of the same type as the dataValueField

1

I have a list of items called id_waers.

var id_waers = db.MONEDAs.Where(m => m.ACTIVO == true).ToList();

where the id is "WAERS".

I will create a SelectList with id_waers values, "WAERS" as id and the text to show will be the id too and show the "USD" value as selected

ViewBag.MONEDA = new SelectList(id_waers, "WAERS", dataTextField: "WAERS", selectedValue: "USD");

default value

I have another options

2
  • 1
    Don't just blurt out code. Add some text to explain how it addresses the OP's question. This will improve the long term value of the answer and will prevent it from being deleted by review. Apr 3, 2018 at 20:25
  • Thanks! @NightOwl888 Aug 7, 2020 at 14:54
0

Yes this properties are read only, following code should work:

        SelectList selectList = new SelectList(Service.All, "Id", "Name");
        foreach (SelectListItem item in selectList.Items)
        {
            if (item.Value == yourValue)
            {
                item.Selected = true;
                break;
            }
        }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.