108,524 views 1 on YTPak
195 13

Published on 15 Jun 2013 | over 5 years ago

In this video we will discuss implementing a checkbox list in asp.net mvc. We will be using table "tblCity" for this demo.

Using the link below for, Sql script to create table tblCity

Add ADO.NET data model, to retrieve data from database table tblCity

Right click on the "Controllers" folder, and add a "HomeController". Include the following 2 namespaces in "HomeController"
using MVCDemo.Models;
using System.Text;

Copy and paste the following code.
public ActionResult Index()
SampleDBContext db = new SampleDBContext();
return View(db.Cities);

public string Index(IEnumerable[City] cities)
if (cities.Count(x =] x.IsSelected) == 0)
return "You have not selected any City";
StringBuilder sb = new StringBuilder();
sb.Append("You selected - ");
foreach (City city in cities)
if (city.IsSelected)
sb.Append(city.Name + ", ");
sb.Remove(sb.ToString().LastIndexOf(","), 1);
return sb.ToString();

Right click on the "Views" folder, and a "Home" folder. Right click on the "Home" folder and "EditorTemplates" folder.

Right click on "EditorTemplates" folder - Add - View. In the "Add View" dialog box, set
View Name = City
View Engine = Razor
and click "Add".

Copy and paste the following code in "City.cshtml"
@model MVCDemo.Models.City

ViewBag.Title = "City";

@Html.HiddenFor(x =] x.ID)
@Html.HiddenFor(x =] x.Name)

@Html.CheckBoxFor(x =] x.IsSelected)

@Html.DisplayFor(x =] x.Name)

Please Note: Put the templates in "Shared" folder, if you want the "Templates", to be available for all the views.

Right click on the "Index" action method in "HomeController", and select "Add View" from the contex menu. Set
View Name = Index
View Engine = Razor and click "Add"

Copy and paste the following code in "Index.cshtml"
@model IEnumerable[MVCDemo.Models.City]
ViewBag.Title = "Index";
[div style="font-family:Arial"]

@using (Html.BeginForm())
[br /]
[input type="submit" value="Submit" /]

Text version of the video


All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic
Customize Your Hybrid Embed Video Player!

6-digit hexadecimal color code without # symbol.


Report video function is under development.


Loading related videos...