Hello Friends in this tutorial we are going to see how to Bind Dropdownlist using Entity Framework in ASP.NET MVC. As we are already familiar with Entity Framework from our previous sessions.

After completing this tutorial we would be able to understand:

  • How to Bind Dropdownlist using Entity Framework in ASP.NET MVC?

How to Bind Dropdownlist using Entity Framework in ASP.NET MVC?

Create Database and table in SQL Server.

Step 1 – Open SQL Server and Create a Database called EntityDemo using the following script.

CREATE DATABASE EntityDemo

Step 2 –  Create a table called Employee using the following script.

CREATE TABLE Employee
(
ID BIGINT PRIMARY Key,
Name VARCHAR(100)
)

Step 3 – Now insert few records.

INSERT INTO Employee(ID,Name) VALUES (1,'Vinod')
INSERT INTO Employee(ID,Name) VALUES (2,'Manoj')
INSERT INTO Employee(ID,Name) VALUES (3,'Ansh')
INSERT INTO Employee(ID,Name) VALUES (4,'Ritesh')
INSERT INTO Employee(ID,Name) VALUES (5,'Vishwas')

Now we are done with our SQL Server.

Step 4- Open Visual Studio and create an ASP.NET MVC application called  BindDropDownEntityFramework.

Bind Dropdownlist using Entity Framework

Step 4- Choose and Empty template and Click OK.

Step 5- First of all, we need to install Entity Framework using nuget package manager as shown in the below image.

Bind Dropdownlist using Entity Framework

Step 6 –  Choose Entity Framework and Click install.

Bind Dropdownlist using Entity Framework

Bind Dropdownlist using Entity Framework

Step 7 – After successful installation right click on the Model folder and Create a class called Employee with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BindDropDownEntityFramework.Models
{
[Table("Employee")]
public class Employee
{
[Key]
public Int64 ID { get; set; }
public string Name { get; set; }
}
}

Step 8 –  Right Click on the Model folder and create a class called Employees with the following code.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BindDropDownEntityFramework.Models
{
public class Employees
{
public SelectList EmployeeList { get; set; }
public Int64 ID { get; set; }
}
 
}

Step 9 – Right Click on the Model folder and create DbContext Class called EmployeeDbContext with the following code.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace BindDropDownEntityFramework.Models
{
public class EmployeeDbContext:DbContext
{
public DbSet<Employee> Employee { get; set; }
 
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<EmployeeDbContext>(null);
base.OnModelCreating(modelBuilder);
}
}
}

The EmployeeDbContext class contains a DbSet property that will return Employee.

Step 10 – Add the connection string to the web.config file.

<connectionStrings>
<add name="EmployeeDbContext" connectionString="data source=DESKTOP-9VC12LL\SQLEXPRESS;Initial Catalog=MYDB;integrated security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

Note:

Name of the DbContext class and Connection String key name should be same.

Step 11 – Right-click on the Controller folder and add a Controller called HomeController and write following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BindDropDownEntityFramework.Models;

namespace BindDropDownEntityFramework.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
EmployeeDbContext objEmployeeDbContext = new EmployeeDbContext();

public ActionResult Index()
{
List<Employee> lstEmployees = objEmployeeDbContext.Employee.ToList();
SelectList lstEmployeeSelectList = new SelectList(lstEmployees, "ID", "Name");

Employees objEmployeeList = new Employees();
objEmployeeList.EmployeeList = lstEmployeeSelectList;

return View(objEmployeeList);
}

}
}

Step 12 –  Right Click on the Index Action method and Create a View as shown below.

Bind Dropdownlist using Entity Framework

Step 13 –  Write the following code to Index.cshtml file.

@model BindDropDownEntityFramework.Models.Employees

@{
 ViewBag.Title = "Index";
}

<h2>Bind Dropdownlist using Entity Framework in ASP.NET MVC</h2>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" >
<div class="row">
 <div class="col-md-2">

@Html.DropDownList("Mobiledropdown1", Model.EmployeeList, "Select", new { @class = "form-control" })
 </div>
</div>

Run the application and the output window will be:

Bind Dropdownlist using Entity Framework

View More:

Conclusion:

I hope you understand how to Bind Dropdownlist using Entity Framework in ASP.NET MVC. I would like to have your valuable comments and Feedback.

Thank You,

Leave a Comment