Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum to Dropdown in MVC 5

I have a nullable enum defined for a person title and used in a person model.

public enum Titles
{
    Mr=0,
    Mrs=1,
    Miss=2,
    Dr=3
}
[Required(ErrorMessage="Please supply the title.")]
[Display(Name = "Title")]
public Titles Title { get; set; }

When I add this property into a create or edit view using the HTML Helper

@Html.EnumDropDownListFor(model => model.Title)

the control renders as expected with the enum values within it.

However, when I choose to edit an existing person, the title enum doesn't show the current title. It shows an empty entry at the top of the DropDownList.

However, if I remove the nullable, it always shows the first item in the enum.

Any ideas how I get the DropDownList to display the correctly chosen enum item for the person I am editing?

Many thanks,

Jason.

like image 283
Jason James Avatar asked Mar 23 '14 08:03

Jason James


People also ask

How do you create a DropdownList from an enum in ASP NET MVC?

Create an enum, ActionType, under the Models folder as in the following code snippet. I populate the DropDownList using a SelectListItem type list. So we need to first create a list from enum and thereafter that list binds with the DropDownListFor method of the HTML Helper class on the view.

What is enum in MVC?

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).

How can use static dropdown in MVC?

Binding MVC DropDownList with Static Values Just add an Html helper for DropDownList and provide a static list of SelectListItem. The values added as SelectListItem will be added and displayed in the DropDownList. In this way, you do not need to add anything to Controller Action.


1 Answers

I just made a simple test.

You mentioned that you have a nullable Enum, but for that you need public Titles? Title { get; set; }

and using this Model:

public class TestViewModel
{
    [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Please supply the title.")]
    [System.ComponentModel.DataAnnotations.Display(Name = "Title")]
    public Title? Title { get; set; }
}
public enum Title
{
    Mr = 0,
    Mrs = 1,
    Miss = 2,
    Dr = 3
}

with this ActionResult

public ActionResult Test()
{
    var model = new List<Models.TestViewModel>();

    model.Add(new TestViewModel() { Title = Title.Miss });
    model.Add(new TestViewModel() { Title = Title.Mrs });
    model.Add(new TestViewModel() { Title = null });

    return View(model);
}

and using a simple HTML

@model List<Models.TestViewModel>
@{
    Layout = null;
}

<!DOCTYPE html>    
<html>
<head><title>Test</title></head>
<body>
    @for (int i = 1; i <= Model.Count; i++)
    {
        var title = Model[i-1];
        <div>
            <h2>Model @i</h2>
            @Html.LabelFor(x => title.Title)
            @Html.EnumDropDownListFor(x => title.Title)
            @Html.EditorFor(x => title.Title)
        </div>
    }
</body>
</html>

I get this as result:

enter image description here

Which is exactly what to expect... are you missing something from my example?

like image 186
balexandre Avatar answered Oct 29 '22 00:10

balexandre