How To Bind Html.CheckBoxListFor In ASP.NET MVC

Introduction

In this article we will learn how to bind Html.CheckBoxListFor in  MVC which is html helper to create strongly typed CheckBoxList . So lets start with step by step.
Step 1 : Create an MVC Application.
Now let us start with a step by step approach from the creation of simple MVC application as in the following:
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
  2. "File", then "New" and click "Project..." then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK . 
 Step 2 : Add Model class
Right click on model folder of created MVC application project and add class named CityModel.cs or  as you wish
CityModel.cs
public class CityModel
    {
            //Value of checkbox 
            public int Value { get; set; }
            //description of checkbox 
            public string Text { get; set; }
            //whether the checkbox is selected or not
            public bool IsChecked { get; set; }
        }
    public class CityList
        {
            //use CheckBoxModel class as list 
            public List<CityModel> Cities { get; set; }
        }
Step 2 : Add Controller
Right click on Controllers folder of created MVC application and click add empty Controller named HomeController.cs as

 Now create the generic list and add the records instead of going to database,however you can bind generic list from database but for example we added hard coded records.

 HomeController.cs
using System.Collections.Generic;
using System.Web.Mvc;
using BindStronglyTypedCheckBox.Models;
namespace BindStronglyTypedCheckBox.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<CityModel> obj = new List<CityModel>()
           {
               new CityModel {Text="Latur",Value=1,IsChecked=false },
               new CityModel {Text="Mumbai",Value=2,IsChecked=true },
               new CityModel {Text="Pune",Value=2,IsChecked=false },
               new CityModel {Text="Noida",Value=2,IsChecked=false },
           };
            CityList objBind = new CityList();
            objBind.Cities = obj;
            return View(objBind);
        }
    }
}
Step 3: Add View
Right click on View folder of created MVC application project and add empty view named Index.cshtml as
Now open the Index.cshtml view and write the following code into the view
@model BindStronglyTypedCheckBox.Models.CityList
@{
    ViewBag.Title = "www.compilemode.com";
}
<div class="form-horizontal">

        <h4>Select Preferred work location</h4>
           
            @foreach (var item in Model.Cities)
            {

                @Html.CheckBoxFor(m => item.IsChecked)
                @item.Text
                @Html.HiddenFor(m => item.Value)
                @Html.HiddenFor(m => item.Text) <br />
            }
</div>
Now everything is ready ,run the application then the check box list will be look like as follows .
From all above example we have learned how  bind Html.CheckBoxListFor in ASP.NET MVC.
 Note

  • Do a proper validation such as date input values when implementing.
  • In this example CheckBoxList bound using generic list however you can also bind it using database.
Summary
I hope this article is useful for all readers, if you have a suggestion then please contact me.
To learn ASP.NET MVC step by step please refer following articles
For more MVC article ,please refer www.compilemode.com , MVC section using following link.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode