I'm having a problem displaying selected value with Html.DropDownListFor. I have a view that uses multiple drop downs, all of them get populated, but nothing gets selected. I have same selection choices for every dropdown, but different selected values. Here's my viewmodel:
public class ViewPersonTargetingViewModel
{
public List<PersonTarget> Targets { get; set; }
public SelectList TargetStatuses { get; set; }
}
That's how I make a SelectList:
var statuses = DataAccess.GetPersonTargetStatuses();
TargetStatuses = new SelectList(statuses, "PersonTargetStatusUuid", "PersonTargetStatusName");
My PersonTarget class:
public class PersonTarget
{
public string PersonTargetStatusName { get; set; }
public Guid PersonTargetStatusUuid { get; set; }
//and more
}
And this is how I create a DropDownList:
<%: Html.DropDownListFor(model => model.Targets[k].PersonTargetStatusUuid, Model.TargetStatuses)%>
where k is an iteration through Targets List.
I use the same SelectList to populate all of the DropDowns, it contains info like this:
for example, when I'm trying to get it to display "B", I give him the correct target status, but it still shows first element ("A") as selected, I even printed out the values I give him, and I get that Model.Targets[k].PersonTargetStatusUuid
has the value of: 0b59faaa-514d-4212-93cd-6b0a50b8f151 (It's Guid of "B"), but it still displays "A". I ran out of ideas how to fix this. Any help is greatly appreciated, thanks :).
So finally I've figured out that the problem lied in my single List. It turns out that for some reason, individual dropdowns must be filled from different lists. It all works now :)