CheckBoxList in ASP.Net MVC

Introduction

This article shows how to use a CheckBoxList control in MVC using a HTML helper and binding with LINQ to SQL.

Database structure

Create a table in the database with the name Country. The following is the create table code for the Country table:

  1. CREATE TABLE [dbo].[Country]  
  2.  (  
  3.   [id] [intNOT NULL,  
  4.   [name] [varchar](50) NOT NULL,  
  5.   [IsSelected] [bitNOT NULL  
  6. )  

Adding a LINQ to SQL Class

Step 1

Right-click on the project and select "Add new item", then select Data from the templates.

Step 2

Choose "LINQ to SQL classes" from the list and provide a name. Now after clicking on Add, you can see the DBML file in the project.

Step 3

Drag the Country tables from the database in the Server Explorer.



Create home controller

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using checkboxmvc.Models;  
  7. using System.Text;  
  8.   
  9.   
  10. namespace checkboxmvc.Controllers  
  11. {  
  12.     public class homeController : Controller  
  13.     {  
  14.        
  15.         DataClasses1DataContext db = new DataClasses1DataContext();  
  16.         public ActionResult Index()  
  17.         {  
  18.   
  19.             return View(db.Countries);  
  20.         }  
  21.         [HttpPost]  
  22.         public string Index(IEnumerable<Country> cs)  
  23.         {  
  24.             if (cs.Count(x => x.IsSelected) == 0)  
  25.             {  
  26.                 return "you didn't select any country";  
  27.   
  28.             }  
  29.             else  
  30.             {  
  31.                 StringBuilder sb = new StringBuilder();  
  32.                 sb.Append("you selected..");  
  33.                 foreach (Country cou in cs)  
  34.                 {  
  35.                     if (cou.IsSelected)  
  36.                     {  
  37.   
  38.                         sb.Append(cou.name + ',');  
  39.                     }  
  40.   
  41.                 }  
  42.                 sb.Remove(sb.ToString().LastIndexOf(','), 1);  
  43.                 return sb.ToString();  
  44.                  
  45.             }  
  46.   
  47.         }  
  48.     }  
  49. }  
In the preceding code we have two Actionresult methods, Index and Index(IEnumerable<Country> cs) Index is a [httpget] type and Index(IEnumerable<Country> cs) is a [httppost].

Create EditorTemplates




First we create an Editor Template inside the View folder and EditorTemplate contained in the Country.cshtml file. The file name is the same as the model name and the Country.cshtml file contains the following code.

  1. @model checkboxmvc.Country  
  2.   
  3. @Html.HiddenFor(x=>x.id)  
  4. @Html.HiddenFor(x=>x.name)  
  5.   
  6. @Html.CheckBoxFor(x=>x.IsSelected)  
  7. @Html.DisplayFor(x=>x.name)  
In the preceding code we have used @Html.HiddenFor(). It (@Html.HiddenFor) is the helper that renders a hidden field. @Html.HiddenFor renders input type = "hidden". Helpers are not controls by themself, they simply generate HTML markup. It binds the control to the model property. It created a stongly bound view.

Create a view for showing the CheckBoxList

Right-click on the Index ActionResult method and select Add View. After selecting Add View, a dialog box will open. The view name is Index by default. Now select your model class and click on the OK button.
  1. @model IEnumerable< checkboxmvc.Country>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. @using (Html.BeginForm())  
  10. {  
  11.     <div>  
  12.         @Html.EditorForModel();  
  13.          
  14.          
  15.     </div>  
  16.     <br />  
  17.     <br />  
  18.     <div>  
  19.         <input type="submit" value=" submit" />  
  20.     </div>  
  21.   
  22. }  
The following is the output of the preceding code.



If we will click the Submit button without selecting any CheckBoxList, then the following output will come.


If we will click the Submit button with selecting one or more checkboxes, then the following output will come.




Summary

In this article we learned how to use a CheckBoxList control in MVC using a HTML helper and binding with LINQ to SQL.


Similar Articles