0

I have an enum which looks something like

public enum Department
{
    Admin,
    HR,
    Marketing,
    Sales
}

From this I am trying to create a drop down list in the controller and I do it like

public SelectList GetDepartmentDropDownList(string departmentName = null)
{
    var departments = Enum.GetValues(typeof(Department));
    SelectList ddl = new SelectList(departments);

    return ddl;
}

Which works fine. But as you can see I can pass in the opitional parameter. This optional parameter is passed in when the dropdown value is saved to the database and the user comes back to edit the section. What I am trying to achieve is whatever the user originally selected, when they come back on the edit screen that particular item is select i.e if they selected HR when they come back HR should be selected.

If I do new SelectList(departments, departmentName, departmentName); I get the following error:

DataBinding: Enums.Department' does not contain a property with the name 'HR'

Can someone suggest how to achieve this please.

In the view I am making using of Html.DropDownListFor() as

@Html.DropDownListFor(m => m.Department, Model.DepartmentDdl, "Please select a Department", new { @class = "form-control", @required = "required" })

The property in my model is

public IEnumerable<SelectListItem> DepartmentDdl { get; set; }

And in the controller create action I do

model.DepartmentDdl = GetDepartmentDropDownList();

And in the controller edit action I do

model.DepartmentDdl = GetDepartmentDropDownList(departmentName); //department name here is read from the db
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • Its the value of the property your binding to which determines what is selected. Show your view and the property your binding to (you need to set it in the model before you pass it to the view) –  May 31 '17 at 09:55
  • Just set the value of `Department` to `Department.HR` in the GET method, and it will be selected (there is no point in your parameter in the `GetDepartmentDropDownList()` method –  May 31 '17 at 10:01
  • Possible duplicate of [SO Answer](https://stackoverflow.com/questions/21878673/html-enumdropdownlistfor-showing-a-default-text)? – Mihail Stancescu May 31 '17 at 10:03
  • @StephenMuecke I've tried your suggestion but it still does not select the value. – Izzy May 31 '17 at 10:09
  • @StephenMuecke I've got it to work, would you mind putting it down as an answer and I'll accept it – Izzy May 31 '17 at 10:14

1 Answers1

1

Model binding works by binding to the value of your property. You need to set the value of property Department in the GET method before you pass the model to the view, for example

var model = new YourModel
{
    Department = Department.HR,
    DepartmentDdl = GetDepartmentDropDownList()
};
return View(model);

Note that there is no need for your parameter in the GetDepartmentDropDownList() method. Internally, the DropDownListFor() method builds a new IEnumerable<SelectListItem> and sets the Selected value of each SelectListItem based on the property your binding to (i.e. setting it in the SelectList constructor is ignored).