C# MVC DropDownList using Entity Framework Table Values

The code example below creates a C# MVC DropDownList from database table via the Entity Framework. This article has example code for the Model, View and Controller to make it all work!

This example is based on a list of business unit names that are coming from a database lookup table access via Entity Framework. It is also wired up to persist a selection value using a session value. If session value is present, use it as the Selected item on the list.

This example came from a real project so I’ve had to replace some original code related to Entity and Model name referenced with HARDCODEDVALUES or generic reference like, modelName, you can replace with your own values.

If you are struggling with this as I was for a bit this example might be just what you need to get you over the code hump.

Here are a couple of references that cover the details of the larger overall process of wiring up the Entity Framework for your project that I’m skipping over in the article.

C# MVC DropDownList using Entity Framework

When you get through all the big stuff covered in the links below you still might end up stuck on the little details of implementing the C# MVC drop down list. That is what the intent of this article is.

Model

In the example below we create a public property called BusinessUnitList in our model that the page is using to access the C# controller values.

BusinessUnitList needs to be instantiated as an IEnumerable of type “SelectedListItem” to store our drop down list values.

The IEnumerable exposes an enumerator, which supports a simple iteration over a non-generic collection. In simple terms, IEnumerable, just adds methods and properties like an index value for each item and methods to loop through a list of items. That kind of stuff.

public IEnumerable<SelectListItem> BusinessUnitList {get; internal set;}

View

The version of C# MVC I was using has 7 method overloads for the HTML Helper DropDownList . You’ll find all sorts of examples. Just so you know, there are 7 different ways to do this that work. OMG!

This example is pretty strait forward. This code creates a dropdown list and if you have a session value for this Model. On page load, “Please Select” text will be selected.

@Html.DropDownList("Business Unit", Model.BusinessUnitList, "- - - Please Select - - -")

Controller

This was the hardest part of implementing this code.

In the MVC Controller for this View, we create a list to store the drop down list data.

Next, using Entity Framework, we use a dbContext to connect to an already defined Entity to get the data from the BusinessUnit lookup table.

We dump the data in a tempList variable. For reference, the lookup table name replaces the ENTITYMODELNAMEGOESHERE place holder value.

tempList is modeled after the Entity Framework model for the BusinessUnit lookup table in the database.

Next, we need to convert tempList from the BusinessUnit lookup table model in to a list C# MVC can use for the drop down list that is of type SelectListItem that represents the selected item in an instance of the SelectList class.

We set the default value of the drop down list in case of user posted back by storing that value in a session and referencing here in the code, Selected = (m.BusinessRefId == modelName.BusinessUnit).

//Create List of Business Units for the Drop Down List
IEnumerable<SelectItemListItem> businessUnitList = new List<SelectListItem>();
using (ENTITYNAMEHERE = dbContext = new ENTITYNAMEHERE())
    {
        List<ENTITYMODELNAMEGOESHERE> tempList = dbContext.ENTITYMODELNAMEGOESHERE.OrderBy(p => p.Name).ToList();

busniessUnitList = tempList.Select(m => new SelectListItem { Text = m.Name, Value = m.BusinessRefId, Selected = (m.BusinessRefId == modelName.BusinessUnit) });
        modelName.BusinessUnitList = businessUnitList;
    } 

modelName.BusinessUnitList = businessUnitList;