I need to pass a ViewModel to a strongly typed View which will display a multiple-select box using Html.ListBoxFor
. My ViewModel has a List<int>
for the selected values and an IEnumerable<SelectListItem>
for the options.
Is there a better way I could be enumerating the list of options?
private MyDbContext db = new MyDbContext();
public List<int> SelectedPhysicians { get; set; } = new List<int>(1) { 1 };
public IEnumerable<SelectListItem> AllPhysicians
{
get
{
List<SelectListItem> temp = new List<SelectListItem>();
foreach (PhysicianModel p in db.Physicians)
{
temp.Add(new SelectListItem { Value = p.Id.ToString(), Text = p.Name });
}
return temp;
}
}
Also, can you see any long-term reasons I would even need a List<int>
rather than an int[]
? I tend to go with List
because I almost always end up needing more than basic array operations. In this case, however, I'm just retrieving and storing sets of values in the database.
It's occurred to me that all of my models are using Lists for no tangible reason, but I want to be sure before refactoring.