Here I am going to explain the generating drop downl ist and get the values from database table. First of all create one Model class named as Department. This example is explained in code first approach .For this follow the below procedure .In previous articles i am explained code first approach,before seeing this article please see that article,you will get better idea on how to enable migrations,and how to craete data context class.click here to go that article.
To open vs 2010 and to create new project and for code first approach click here
Directly i am going to Model's folder and add department Model class
public class Department
{ [Key]
public int Id { get; set; }
public string Name { get; set;}
}
Change the DataContext class(how to create datacontext clas click here)
include the department model class in datacontext class
add namespace using MvcEmployee.Migrations;
public class DataContext:DbContext
{
public DataContext() : base("EmployeeDb")
{
}
public DbSet<Employee> employee { get; set; }
public DbSet<Department> department { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
modelBuilder.Entity<Employee>().ToTable("employee");
modelBuilder.Entity<Department>().ToTable("department");
}
}
{
public DataContext() : base("EmployeeDb")
{
}
public DbSet<Employee> employee { get; set; }
public DbSet<Department> department { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
modelBuilder.Entity<Employee>().ToTable("employee");
modelBuilder.Entity<Department>().ToTable("department");
}
}
Now go to Home Controller and write action method name it as Dept and write the following code.
create the instance for DataContext class
Add namespace using MvcEmployee.Models;
private DataContext db = new DataContext();
public ActionResult Dept()
{
ViewBag.Departments = new SelectList(db.department, "DeptId", "DeptName");
return View();
}
{
ViewBag.Departments = new SelectList(db.department, "DeptId", "DeptName");
return View();
}
Add view to Dept Action method and write the following in Dept.cshtml
@Html.DropDownList("Departments", "Select Department")
The Departments should be same as Viewbag
attribute name in home controller
Now run the application and you see the following output
There is no records in the Dropdownlist.Now go to database insert few records in department table.(If the table has records ,those are loaded into dropdownlist.)
Now run the application see the following output
tags:dropdownlist in mvc ,dropdownlist in mvc 3,dropdownlist in mvc 4,dropdownlist in mvc razor,dropdownlist in mvc get the values from database,dropdownlist in asp.net mvc, dropdownlist in asp.net mvc 5,generate dropdownlist in asp.net mvc,how to create dropdownlist in mvc 4,how to create dropdownlist in mvc5