Home Selected property on SelectListItem not being set by SelectList constructor
Reply: 0

Selected property on SelectListItem not being set by SelectList constructor

user925
1#
user925 Published in April 20, 2018, 10:52 am

I'm creating a SelectList to pass to a Html.DropDownList, but the correct value doesn't seem to be populating

var x = new SelectList(new[] { 
    new SelectListItem() { Value="1", Text="Vanilla"}, 
    new SelectListItem() { Value="2", Text="Chocolate"},
    new SelectListItem() { Value="3", Text="Strawberry"}
}, "Value", "Text", "2");

SelectList should take a constructor that takes in the selectedValue:

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

But this only sets the SelectedValue on the entire collection. The individual SelectListItems haven't been set as Selected

Do I need to iterate over them individually and set selected for each as in this question?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.458082 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO