Web
Analytics
Populate DropDownList using Entity Framework in ASP.NET MVC using Razor | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Step1: Create new MVC Project.

Step2 . Create Database Table . In this example I created a table “categories” which have three fields

CategoryId int primarykey

CategoryName varchar(50)

IsSelected bool




Step 3: Create connectionstring in web.config.

Step 4: Create Model Class “Category.cs”

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

namespace PopulateDropDown.Models
{
 [Table("Categories")]
 public class Category
 {
 [Key]
 public int CategoryId { get; set; }
 public string CategoryName { get; set; }
 public bool? IsSelected { get; set; }
 }
}

Step 5: Create DataContext Class "DAL.cs"

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

namespace PopulateDropDown.Models
{
 public class DAL: DbContext
 {
 public DAL() : base("conn") { }
 public DbSet