0
votes

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:

  • Text: A Value: b2e4a939-06d8-4052-8abf-5c1b2a5e8591
  • Text: B Value: 0b59faaa-514d-4212-93cd-6b0a50b8f151
  • Text: C Value: 5575f3cf-33f8-41df-be73-a176a3db5ed5

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 :).

3

3 Answers

0
votes

You need to set SelectedValue property of SelectList. For example like this:

<%: Html.DropDownListFor(model => model.Targets[k].PersonTargetStatusUuid, new SelectList(Model.TargetStatuses, "PersonTargetStatusUuid", "PersonTargetStatusName", Model.Targets[k].PersonTargetStatusUuid)) %>
0
votes

I made a quick & dirty little trial program and it worked perfectly.

You mentioned it in your question, but I'd double check:

  • The HTML of your select list has the appropriate values & names
  • Your PersonTargetStatusUuid is correct

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var colours = new List<CarColour>() { new CarColour() { Id = Guid.NewGuid(), Colour = "Red" }, new CarColour() { Id = Guid.NewGuid(), Colour = "Blue" }, new CarColour() { Id = Guid.NewGuid(), Colour = "Green" } };
        ViewData["Colours"] = new SelectList(colours, "Id", "Colour");

        var car = new Car() { Id = Guid.NewGuid(), Name = "GreenCar", Colour = colours.First(cc => cc.Colour == "Green") };
        return View(car);
    }
}

public class Car
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public CarColour Colour { get; set; }
}

public class CarColour
{
    public Guid Id { get; set; }
    public string Colour { get; set; }
}

Index:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CarTest.Controllers.Car>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <%: Html.LabelFor(m => m.Id) %>
    <%: Html.TextBoxFor(m=> m.Id) %>

    <%: Html.LabelFor(m => m.Name) %>
    <%: Html.TextBoxFor(m=> m.Name) %>

    <%: Html.LabelFor(m => m.Colour) %>
    <%: Html.DropDownListFor(m=> m.Colour.Id, ViewData["Colours"] as IEnumerable<SelectListItem>) %>

</asp:Content>
0
votes

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 :)