Mvc check boxes

How to show checkbox selected items in MVC?

We can show checkbox items from multiple items whatever we select.

Cshtml Page:

 <divclass="form-horizontal"> 
  <h4>Select Preferred work location</h4> 
 for (inti = 0; i<Model.Cities.Count; i++) 
 @using (Html.BeginForm()) 
   { 
 @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/> 
  } 
 <divclass="col-md-offset-2 col-md-10"> 
 <inputtype="submit"value="Save"class="btnbtn-default"/> 
 </div> 

 @ViewBag.Loc 
  } 
 </div> 

Controller Page:

publicActionResult Index()
         {
 //Add Records into generic list
  List<CityModel>obj = new List<CityModel>() 
  { 
 newCityModel {Text="Swimming",Value=1,IsChecked=false }, 
 newCityModel {Text="Sketting",Value=2,IsChecked=true }, 
 newCityModel {Text="Painting",Value=2,IsChecked=false }, 
 newCityModel {Text="PalyingCricket",Value=2,IsChecked=false }, 
     }; 
 CityListobjBind = newCityList(); 
 objBind.Cities = obj; 
 return View(objBind); 
  }   

Leave a comment

Your email address will not be published. Required fields are marked *