Getting Started With Enum Support in MVC 5 View

Introduction

Microsoft has released an updated version of Visual Studio 2013, Visual Studio 2013 Update 1. There are various updated features in this version. I have also described them in the New Release Notes of Visual Studio 2013.

In that context, let's see some new release notes of MVC 5.1 and Web API 2.1 in the following points:

  • ASP.NET MVC 5.1
    • Attribute Routing
    • Bootstrap Support for editor templates
    • Enum Support in Views
    • Unobtrusive validation for Min/MaxLength Attributes
  • ASP.NET Web API 2.1
    • Global error handling
    • Attribute routing
    • IgnoreRoute support
    • Better support for async filters
  • ASP.NET Web Pages 3.1 Bug fixes

Getting Started

Now you are about to start to install this release and I've already mentioned in my previous article how to update your Visual Studio to the latest version. There are some features that require you to update your Visual Studio too but most of them are updated from the NuGet Package Manager. You might however update your Visual Studio. Its better to always update, right?

So, go to my previous article as I mentioned above in this article to update your Visual Studio, whether it is Visual Studio 2012 or Visual Studio 2013.

Working With MVC App

Now, I am developing an ASP.NET application using MVC Project Template to work with the Enum support in the Views. We can check out both aspects or say with Enum support or without Enum support.

So, let's get started with the following procedure.

Step 1: Create an application with the MVC Project Template.

Mvc Prooject Template

Step 2: In Solution Explorer, right-click on the project to open the Manage NuGet Packages.

Manage NuGet Packages

Step 3: There are various types of updates available here.

Update NuGet Packages

Note: I am updating all that is to be updated because this is not the real project. You can select only those updates that are relevant to your project to update.

Installing and Updating the Packages in Application

After installing, you can see your Packages.config is also updated:

Updated Packages Configuration File

Creating Enum Support on Model

Step 1: Now create a class in the model named Student. Use the code below:

namespace NewMvcDemo.Models

{

    public enum Gender

    {

        Male,

        Female

    }

    public class Student

    {

        public int ID { getset; }

        public string Name { getset; }

        public Gender Sex { getset; }

        public string City { getset; }

        public string State { getset; }

    }

}

In the code above, you can see that I've created some properties related to the Student entity and also generated an Enum property for the Gender for the Sex property.

Step 2: Scaffold the model with a new controller.

Add New Scaffolded Item

Step 3: Now run the application and open the controller.

Create Student in Mvc App

Hey, where are the Enum values (Male, Female)? Just hang on. We do not update the view (Create.cshtml). Proceed to the next section

Creating Enum Support on a View

In this section we'll create the Enum support for the view using the following procedure.

Step 1: As you can see that the scaffolded view uses the general code for the Sex Property, in other words @Html.EditorFor.

Create View in Mvc App

Step 2: Now we'll change it with the following code:

<div class="col-md-10">

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

     @Html.ValidationMessageFor(model => model.Sex)

</div>

That's it. We've added Enum support for the View

Step 3: Ok, now run the application and open the controller

Enum Support in View of MVC 5

Summary

This article described how to update Visual Studio and work with Enum support in MVC 5 View. Thanks for reading and stay updated.