Introduction
In this article we will learn how to get the check box checked values in a MVC controller . So lets start with step by step.
In this article we will learn how to get the check box checked values in a MVC controller . So lets start with step by step.
Step 1 : Create an ASP.NET MVC Application.
Now let us start with a step by step approach from the creation of simple MVC application as in the following:
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "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
Now write the following code into the CityModel.cs class
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 3 : Add Controller
Right click on Controllers folder of created MVC application and click add empty Controller named HomeController.cs asNow 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
Index.cshtml as
Now open the Index.cshtml view and write the following code into the view
HomeController.cs
public class HomeController : Controller { // GET: Home public ActionResult Index() { //Add Records into generic list 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); } //Post and get checkbox checked records [HttpPost] public ActionResult Index(CityList Obj) { StringBuilder sb = new StringBuilder(); foreach (var item in Obj.Cities) { if (item.IsChecked) { //append each checked records into StringBuilder sb.Append(item.Text+","); } } //store location into viewbag ViewBag.Loc = "Your preferred work locations are "+ sb.ToString(); //return location view to display checked records using viewbag return View("Locations"); } public ActionResult Locations() { return View(); } }
Step 4: Add View
Right click on View folder of created MVC application project and add empty view namedIndex.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> @using (Html.BeginForm()) { for (int i = 0; i < Model.Cities.Count; i++) { @Html.CheckBoxFor(m => Model.Cities[i].IsChecked) @Model.Cities[i].Text @Html.HiddenFor(m => Model.Cities[i].Value) @Html.HiddenFor(m => Model.Cities[i].Text)<br /> } <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> @ViewBag.Loc } </div>
Now select Pune and Noida then output will be as follows
Note
Summary
- 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.
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
- ActionResult in ASP.NET MVC
- Creating an ASP.NET MVC Application
- CRUD Operations In ASP.NET MVC 5 Using ADO.NET
- Convert DataTable To Generic List In ASP.NET MVC .
- How To Pass Stored Procedure Using Dapper.NET In MVC
- Show Confirm Alert Box on ActionLink Click In ASP.NET MVC.
- Convert DataTable To Generic List Using LINQ In ASP.NET MVC.
- How to Change ActionLink Text Color In ASP.NET MVC .
- How to Call Another Controller View Using ActionLink In ASP.NET MVC .
- Convert DataTable To Generic List Using AsEnumerable In ASP.NET MVC .
- How To Pass Generic List Using Dapper In MVC
Post a Comment