-1

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:

Expected

But I get this

enter image description here

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!

enter image description here

ProfessorChaos
  • 201
  • 3
  • 12
  • did you tried somthing obviosu like changing value to int value of enum? – Selvin Nov 27 '19 at 11:27
  • not mention that question was already asked multiple times – Selvin Nov 27 '19 at 11:28
  • I'm sorry, I couldn't find any answers. Would you mind posting a link? – ProfessorChaos Nov 27 '19 at 11:29
  • first result of "dropdownlist binding enum in mvc" ... seriously, there should be some "googling" test before people post question on SO – Selvin Nov 27 '19 at 11:51
  • Please read the edited part of my post. I'm sorry this caused you frustration <3 – ProfessorChaos Nov 27 '19 at 12:01
  • EditorTemplates that's what first result of "html.dropdownlistfor in foreach" returns – Selvin Nov 27 '19 at 12:07
  • Thanks Selvin, that gives me a lead. I googled for things like "html.dropdownlistfor list" and "html.dropdownlistfor array" for quite a while, but yeah It's quite silly I couldn't find out for myself. Guess I got stuck in a loop. – ProfessorChaos Nov 27 '19 at 12:21
  • Possible duplicate of [How do you create a dropdownlist from an enum in ASP.NET MVC?](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) – Tassadaque Nov 28 '19 at 11:45
  • Tassadaque, thank you, but please read the entire post. My issue isn’t with creating drop downs from enums. – ProfessorChaos Nov 30 '19 at 10:47

2 Answers2

0

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 i.e. 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.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • Thanks, but code works fine as long as I'm not using a collection of the ViewModel, so the referencing shouldn't be off, or am I missing something? Neither changing the value to the int-value of gender, nor setting the Student.Gender-property to string fixed this issue. – ProfessorChaos Nov 27 '19 at 15:52
0

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) });
ProfessorChaos
  • 201
  • 3
  • 12