77,536 views 194 on YTPak
137 9

Published on 15 Jun 2013 | over 3 years ago

Link for code samples used in the demo

Link for csharp, asp.net , ado.net , dotnet basics, mvc and sql server video tutorial playlists

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" /]

Loading related videos...