简体   繁体   中英

Dropdownlistfor List<Model> in ASP.NET MVC

I'm iterating through a list of my Student-viewmodel and creating DropDownLists for each student for their gender. The DownDownList renders fine, but the currently selected value isn't selected by default. (Please read entire post, my problem only occours when using a List of the ViewModel).

This is what I expect to be output:

预期的

But I get this

在此处输入图片说明

ViewModel

    public class Student
{
    public string Name { get; set; }
    public Gender Gender { get; set; }
    public SelectList SelectList { get; set; }
}

public enum Gender
{
    Male,
    Female,
}

The controller-code

        public ActionResult Index()
    {
        List<SelectListItem> selectLists = new List<SelectListItem>();
        foreach (var gender in Enum.GetValues(typeof(Gender)))
            selectLists.Add(new SelectListItem() { Text = gender.ToString(), Value = gender.ToString() });

        List<Student> viewModel = new List<Student>();
        viewModel.Add(new Student() {Name = "Joe", Gender = Gender.Male, SelectList = new SelectList(selectLists, "Value", "Text") });
        viewModel.Add(new Student() {Name = "Jane", Gender = Gender.Female, SelectList = new SelectList(selectLists, "Value", "Text") });
        return View(viewModel);
    }

View-code

@using DropDownListDemo.Models;
@model List<Student>

<div class="row">
@foreach(var item in Model)
{
    @Html.Label(item.Name)
    <br />
    @Html.LabelFor(i => item.Gender)
    @Html.DropDownListFor(i => item.Gender, item.SelectList, "Select Gender")
    <br />
}

Does anyone have any ideas? Thanks a lot!

EDIT

My initial post wasn't clear enough, so I'll elaborate a bit. The DropDownLists work just fine if i don't use a List of the ViewModel. Please take a look at this code below, which works just fine.

Controller without multiple Students

        public ActionResult Index()
    {
        List<SelectListItem> selectLists = new List<SelectListItem>();
        foreach (Gender gender in Enum.GetValues(typeof(Gender)))
            selectLists.Add(new SelectListItem() { Text = gender.ToString(), Value = gender.ToString() });

        return View(new Student() { Name = "Jane", Gender = Gender.Female, SelectList = new SelectList(selectLists, "Value", "Text") });
    }

View without multiple Students

@using DropDownListDemo.Models;
@model Student
<div class="row">

@Html.Label(Model.Name)
<br />
@Html.LabelFor(m => m.Name)
@Html.DropDownListFor(m => Model.Gender, Model.SelectList, "Select Gender")
</div>

Outputs as expected!

在此处输入图片说明

Your DropdownListFor will show the select list item that has a Value referencing the same object that is in the property that it is bound to ie Student.Gender in your case.

When you create your select list items, you are doing Value = gender.ToString() , but gender.ToString() != gender.

Try setting value as just Value = gender which I would imagine would work, as it would use the int value referenced by the enum.

Alternatively, change your Student.Gender property to be string .

After further investigation I've found the following workaround, which still doesn't answer my initial question, but it works just as good in my case - though it still bothers me that I didn't get any closure to WHY the previous code didn't work.

When initializing the SelectList we can pass in a fourth parameter: selectedValue.

viewModel.Add(new Student() { Name = "Joe", Gender = Gender.Male, SelectList = new SelectList(selectLists, "Value", "Text", Gender.Male) });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM