Part 38 CheckBoxList in asp net mvc



Part 38 CheckBoxList in asp net mvc

Part 38   CheckBoxList in asp net mvc

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
http://csharp-video-tutorials.blogspot.com/2013/06/part-38-checkboxlist-in-aspnet-mvc.html

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

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.
[HttpGet]
public ActionResult Index()
{
SampleDBContext db = new SampleDBContext();
return View(db.Cities);
}

[HttpPost]
public string Index(IEnumerable[City] cities)
{
if (cities.Count(x =] x.IsSelected) == 0)
{
return “You have not selected any City”;
}
else
{
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”]
[h2]Index[/h2]

@using (Html.BeginForm())
{
@Html.EditorForModel()
[br /]
[input type=”submit” value=”Submit” /]
}
[/div]

Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/06/part-38-checkboxlist-in-aspnet-mvc.html

Slides
http://csharp-video-tutorials.blogspot.com/2013/09/part-38-checkboxlist-in-aspnet-mvc.html

All ASP .NET MVC Text Articles
http://csharp-video-tutorials.blogspot.com/p/aspnet-mvc-tutorial-for-beginners.html

All ASP .NET MVC Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-mvc-slides.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

Comments are closed.