J.Cart
J.Cart

Reputation: 572

List<SelectListItem> Selected Value Not Showing for @Html.DropDownListFor

I have a razor page. On load it goes through the below function and creates a list of SelectListItem:

        List<SelectListItem> groups = new List<SelectListItem>();
        for (int i = 0; i < Model.List.key.Count(); i++)
        {
            bool selected = false;
            if(Model.List.key[i] == Model.rkey)
            {
                selected = true;
            }
            groups.Add(new SelectListItem
            {
                Text = Model.List.description[i],
                Value = Model.List.key[i].ToString(),
                Selected = selected
            });
        }

It is properly setting the Selected value to true when the keys match, however it is not displaying the selected value:

@Html.DropDownListFor(model => Model.groupTypeList, groups, new { @class = "form-control", onchange = "chosenGroup(this.value)", id = "groupTypeList" })

The above ddl shows all the values that were added, but does not get set to the Selected value. I tried the below but it then show the selected value twice:

@Html.DropDownListFor(model => Model.groupTypeList, groups, groups.First(x => x.Selected == true).Text, new { @class = "form-control", onchange = "chosenGroup(this.value)", id = "groupTypeList" })

Upvotes: 0

Views: 74

Answers (1)

Juli&#225;n
Juli&#225;n

Reputation: 1386

You can try this:

Model:

public class Group
{
    public IEnumerable<Key> List { get; set; }

    public int rkey { get; set; }
}

public class Key
{
    public int key { get; set; }

    public string desc { get; set; }
}

Action Controller:

public ActionResult Index()
{
    Group g = new Group
    { 
        List = new List<Key>() 
        { 
            new Key { key = 1, desc = "Test" } ,
            new Key { key = 2, desc = "Test2" },
            new Key { key = 3, desc = "Test3" },
            new Key { key = 4, desc = "Test4" },
            new Key { key = 5, desc = "Test5" }
        } 
    };

    g.rkey = 4;

    return View(g);
}

View:

@model WebMVC.Models.Group

<div> 

    @{

        List<SelectListItem> groups = new List<SelectListItem>();

        for (int i = 0; i < Model.List.Count(); i++)
        {
            bool selected = false;

            if (Model.List.ElementAt(i).key == Model.rkey)
            {
                selected = true;
            }

            groups.Add(new SelectListItem {  Text = Model.List.ElementAt(i).desc, Value = Model.List.ElementAt(i).key.ToString(), Selected = selected });
        }

    }

    <label>Select option:</label>

    @Html.DropDownList("MyDropDownList", groups, null, new { @class = "form-control", onchange = "chosenGroup(this.value)", id = "groupTypeList" })

</div>

Another way is using DropdownListFor directly without the razor validation in the view:

@Html.DropDownListFor(x => x.rkey, new SelectList(Model.List, "key", "desc"), null, new { @class = "form-control", onchange = "chosenGroup(this.value)", id = "groupTypeList" })

Upvotes: 1

Related Questions