Populate dropdownlist from Database using EF in MVC3

Create new mvc3 web application.

In Models folder add new item select ‘Data’ > ‘ADO.NET Entity Data Model’ named as ‘Model1.edmx’

‘Generate from database’ select database and tables to create a connection string .

It will add all tables into Model1.edmx file.

Now we need to create DbContext for that open `Model1.edmx` right click select on ‘add code generation item’

Select code > ‘ADO.NET DbContext Generator’ named as ‘Model1.tt’

Going further add new class in to Models folder named as ‘SecurityIdentifierMappingViewModel.cs’

public class ViewModel
public Mapping MappingControls { get; set; }

public List PricingSecurities { get; set; }
public List CUSIPs { get; set; }

Need to create another class into Models folder named as ‘DataRepository’

in which going to write methods to populate dropdowns

public class DataRepository
public mydataEntities dbContext = new mydataEntities();

public List GetPricingSecurityID()
var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing.AsEnumerable()
select new SelectListItem
Text = m.PricingSecurityID.ToString(),
Value = m.PricingSecurityID.ToString()

return pricingSecurityID.ToList();

public List GetCUSIP()
var cusipID = (from m in dbContext.StateStreet_DailyPosition_Second.AsEnumerable()
select new SelectListItem
Text = m.CUSIP.ToString(),
Value = m.CUSIP.ToString()

return cusipID.ToList();

Now create HomeController.cs into folder ‘Controllers’

public class HomeController : Controller
// GET: /Home/

mydataEntities dbContext = new mydataEntities();
DataRepository objRepository = new DataRepository();

public ActionResult Index(string userAction , int id = 0)
var mappingobj = new ViewModel();
mappingobj.MappingControls = dbContext.Mappings.Find(id);

mappingobj.MappingControls = new SecurityIdentifierMapping();
mappingobj.MappingControls.PricingSecurityID = 0;
mappingobj.MappingControls.CUSIP = string.Empty;

mappingobj.PricingSecurities = objRepository.GetPricingSecurityID();
mappingobj.CUSIPs = objRepository.GetCUSIP();

return View(mappingobj);

Now inside Views folder create Index.cshtml

@model Mapping.Models.ViewModel
ViewBag.Title = “Index”;


@*Partial control for all controls*@


I have created a partial view called ‘_ControlPartial’ as we can use it in other Views too.

Code : _ControlPartial.cshtml

@model Mapping.Models.ViewModel
@using (Html.BeginForm())


@Html.Label(“Pricing SecurityID”)
@Html.HiddenFor(model => model.MappingControls.Id)
@Html.DropDownListFor(model => model.MappingControls.PricingSecurityID,
new SelectList(Model.PricingSecurities, “Value”, “Text”),
“Select SecurityID”
@Html.ValidationMessageFor(model => model.MappingControls.PricingSecurityID)
@Html.Label(“CUSIP ID”)
@Html.DropDownListFor(model => model.MappingControls.CUSIP,
new SelectList(Model.CUSIPs, “Value”, “Text”),
“Select CUSIP”
@Html.ValidationMessageFor(model => model.MappingControls.CUSIP)
@Html.TextBoxFor(model => model.MappingControls.Calculation)
@Html.ValidationMessageFor(model => model.MappingControls.Calculation)

Can also bind with following code

@using (Html.BeginForm(“Mapping”, “Home”))
@Html.DropDownList(“SecurityID”, Model.PricingSecurityID, “– Select SecurityID –“)

@Html.DropDownList(“CUSIPID”, Model.PricingSecurityID, “– Select CUSIPID –“)


Thats it we are ready to F5.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s