In This Blog, you can find the source code for binding dropdownlist with database in asp.net mvc.
Binding DropDownList With Database In ASP.Net MVC
Home Controller Source Code
using Binding_DDL_MVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Binding_DDL_MVC.Controllers
{
public class HomeController : Controller
{
practiceEntities db = new practiceEntities();
public ActionResult Index()
{
List<employee> empList = db.employees.ToList();
ViewBag.EmployeeTbl = new SelectList(empList, "name", "name");
return View();
}
}
}
Index.cshtml Source Code
@model Binding_DDL_MVC.Models.employee
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.name, ViewBag.EmployeeTbl as SelectList,"--Select--", new { @class = "form-control" } )
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.joining_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.joining_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.joining_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Context Class Source Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Binding_DDL_MVC.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class practiceEntities : DbContext
{
public practiceEntities()
: base("name=practiceEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<employee> employees { get; set; }
}
}
Employee Model Class Source Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Binding_DDL_MVC.Models
{
using System;
using System.Collections.Generic;
public partial class employee
{
public int id { get; set; }
public string name { get; set; }
public Nullable<System.DateTime> joining_date { get; set; }
}
}
Structure Of Database Table

SQL Query For Database Table
CREATE TABLE [dbo].[employee](
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[joining_date] [datetime] NULL
)
Connection String Present In Web.Config File
<connectionStrings>
<add name="practiceEntities" connectionString="metadata=res://*/Models.Binding_DDL.csdl|res://*/Models.Binding_DDL.ssdl|res://*/Models.Binding_DDL.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-VDTRVCC;initial catalog=practice;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Click Below Link to Download Notes & Source Code Of This Blog
https://www.mediafire.com/file/1ppq4m9jiybf3kb/Binding_DDL_MVC.rar/file
No responses yet