Рет қаралды 179,315
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
csharp-video-tu...
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 KZbin channel. Hope you can help.
/ @aarvikitchen5572
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
csharp-video-tu...
Slides
csharp-video-tu...
All ASP .NET MVC Text Articles
csharp-video-tu...
All ASP .NET MVC Slides
csharp-video-tu...
All Dot Net and SQL Server Tutorials in English
www.youtube.co...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic