How To Bind Checkbox From Database In MVC 5 Using Entity Framework

In this article I will demonstrate how to bind checkbox from database table in mvc5 using entity framework. I will also show selected checkbox value after button submit.

Introduction

In this article I will demonstrate how to bind checkbox from database table in MVC5 using Entity Framework. I will also show selected checkbox value after button submit.

Step 1

Open SQL Server 2014 and create database table to insert data and retrieve data.

  1. CREATE TABLE [dbo].[Languages](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Programming_Language] [nvarchar](50) NULL,  
  4.  CONSTRAINT [PK_Languages] PRIMARY KEY CLUSTERED   
  5. (  
  6.     [ID] ASC  
  7. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  8. ON [PRIMARY]  
  9.   
  10. GO  

Screenshot for database table

ASP.NET

Step 2

Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project 1

ASP.NET

After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name of your project and then click on OK.

Screenshot for creating new project 2

ASP.NET

After clicking on OK, one more window will appear. Choose Empty check on MVC checkbox and click on OK.

Screenshot for creating new project-3

ASP.NET

 

After clicking on OK, the project will be created with the name of MvcCheckBoxList_Demo.

Step 3

Add Entity Framework, right click on Models folder, select Add, then select New Item then click on it.

Screenshot for adding entity framework 1

ASP.NET

After clicking on new item, you will get a window. From there, select Data from the left panel and choose ADO.NET Entity Data Model, give it a name as DBModels (this name is not mandatory; you can give any name) than click on Add.

Screenshot for adding entity framework 2

ASP.NET

After you click on Add, a window wizard will open. Choose EF Designer from database and click Next.

Screenshot for adding entity framework 3

ASP.NET

After clicking on Next, the window will appear. Choose New Connection. Another window will appear. Add your server name if it is local then enter dot (.). Choose your database and click on OK.

Screenshot for adding entity framework 4

ASP.NET

 

Connection will get added. If you wish to save connect as you want. You can change the name of your connection below. It will save connection in web config then click on Next.

Screenshot for adding entity framework 5

ASP.NET

After clicking on NEXT, another window will appear. Choose database table name as shown in the below screenshot, then click Finish. Entity Framework will be added while respective class gets generated under Models folder.

Screenshot for adding entity framework 6

ASP.NET

 

Screenshot for adding entity framework 6

ASP.NET

Following class will be added:

  1. namespace MvcCheckBoxList_Demo.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Language  
  7.     {  
  8.         public int ID { get; set; }  
  9.         public string Programming_Language { get; set; }  
  10.   
  11.         public bool IsCheked { get; set; }  
  12.     }  
  13.   
  14.     public class LanguageModel  
  15.     {  
  16.         public List<Language> Languages { get; set; }  
  17.     }  
  18. }  

Step 4

Right click on Controllers folder select Add then choose Controller as shown in below screenshot.

ASP.NET

 

After clicking on controller a window will appear, choose MVC5 Controller-Empty click on Add.

ASP.NET

 

After clicking on Add another window will appear with DefaultController. Change the name HomeController then click on Add. HomeController will be added under Controllers folder. Remember don’t change the suffix for all controllers, change only highlighted one, and  instead of Default just change Home. As showm in below screenshot.

ASP.NET

Add the following namespace in controller

  1. using MvcCheckBoxList_Demo.Models;  

 

Complete controller code

  1. using MvcCheckBoxList_Demo.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. namespace MvcCheckBoxList_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         [HttpGet]  
  13.         public ActionResult Index()  
  14.         {  
  15.             LanguageModel ProgrammingLanguage = new LanguageModel();  
  16.             using (DBModel db = new DBModel())  
  17.             {  
  18.                 ProgrammingLanguage.Languages = db.Languages.ToList<Language>();  
  19.             }  
  20.             return View(ProgrammingLanguage);  
  21.         }  
  22.         [HttpPost]  
  23.         public ActionResult Index(LanguageModel model)  
  24.         {  
  25.             var selectedLanguage = model.Languages.Where(x => x.IsCheked == true).ToList<Language>();  
  26.   
  27.             return Content(String.Join(",", selectedLanguage.Select(x => x.Programming_Language)));  
  28.         }  
  29.     }  
  30.   
  31. }  

Step 5

Right click on index action method in controller. Add view, a window will appear with default index name unchecked (use a Layout page) and click on Add as show in below screenshot. View will be added in views folder under Home folder with name index.

Screenshot for adding view

ASP.NET

 

Step 6

Click on Tools, select NuGet Package Manager, then choose Manage NuGet Packages for Solution and click on it.

Screenshot for NuGet Package 1

ASP.NET

After that a window will appear, choose Browse type as bootstrap and install package in project. As shown.

Screenshot for NuGet Package 2

ASP.NET

Similarly type JQuery and install latest version of JQuery package in project and Jquery validation file from NuGet then close NuGet Solution.

Screenshot for NuGet Package 3

ASP.NET

 

Keep required bootstrap and jQuery files and delete remaining files if you're not using them. 

Screenshot for NuGet Package 4

ASP.NET

Step 7

Write the following code in index view.

Complete view code

  1. @model MvcCheckBoxList_Demo.Models.LanguageModel  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  7.   
  8. <html>  
  9. <head>  
  10.     <meta name="viewport" content="width=device-width" />  
  11.     <title>Index</title>  
  12.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  13.     <script src="~/scripts/jquery-3.3.1.min.js"></script>  
  14.     <script src="~/scripts/bootstrap.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <div class="container py-4">  
  18.         <h5 class="text-center text-uppercase">How to bind checkbox in mvc5 from database</h5>  
  19.         @using (Html.BeginForm())  
  20.         {  
  21.             <table>  
  22.                 @for (int i = 0; i < Model.Languages.Count; i++)  
  23.                 {  
  24.                     if (i % 4 == 0)  
  25.                     {  
  26.                         @:  
  27.                         <tr></tr>  
  28.                     }  
  29.                     <td>  
  30.                         @Html.CheckBoxFor(x => x.Languages[i].IsCheked, new { @class = "custom-checkbox" })  
  31.                         <label>@Model.Languages[i].Programming_Language</label>  
  32.                         @Html.HiddenFor(x => x.Languages[i].ID)  
  33.                         @Html.HiddenFor(x => x.Languages[i].Programming_Language)  
  34.                     </td>  
  35.                 }  
  36.             </table>  
  37.             <input type="submit" class="btn btn-outline-primary rounded-0" value="Submit" />  
  38.         }  
  39.     </div>  
  40. </body>  
  41. </html>  

Step 8

Run Project ctrl+F5

Screenshot 1

ASP.NET

 

Screenshot 2

ASP.NET

 

Screenshot 3

ASP.NET

 

Conclusion

In this article I have explained how we can dynamically bind checkbox from database in MVC 5 using entity framework.