Hi,
I am trying to create checkboxlist in ASP.NET MVC. I tried it from tutorial provided below
Creating Simple Checkboxlist in MVC 4 Using Razor[
^]
I am facing an issue which I am not able to resolve.
Below is the code for the same.
namespace MvcCheckBox.Models
{
public class CheckModel
{
public int Id{get;set;}
public string Name{get;set;}
public bool Checked{get;set;}
}
}
namespace MvcCheckBox.Controllers
{
public class HomeController:Controller
{
[HTTPGet]
public ActionResult Index()
{
var list=new List<CheckModel>{new CheckModel{Id=1,Name="A",Checked=false},
new CheckModel{Id=2,Name="B",Checked=false},};
return View(list);
}
[HTTPPost]
public ActionResult Index(List<CheckModel> list)
{
return View(list);
}
}
}
//View
@model List<MvcCheckBox.Models.CheckModel>
@{
viewBag.Title="Index";
}
<h2>Index</h2>
@using(Html.BeginForm())
{
for(var i= 0;i<Model.Count;i++)
{
<table><tr>
<td>
@Html.HiddenFor(it=>it[i].Id)
@Html.HiddenFor(it=>it[i].Name)
</td>
<td>
@Html.CheckBoxFor(it=>it[i].Checked,new{Style="vertical-align:3px"})
</td>
</tr></table>
}
<input id="Submit1" type="submit" value="Submit"/>
}
Issue:
When I run the code output is exactly how it should be i.e. name and checkbox is rendered.
After checking checkbox and clicking on submit, it doesn't displays name, i.e. during postback checkbox is rendered without name. After applying breakpoint, list shows "Name" property as empty.
I am not able to understand the problem.
I'm new to ASP.NET MVC, so still experimenting with MVC.
Thanks & Regards
Hari
What I have tried:
I don't know what to try or search. Any help/suggestion are welcome and will try to implement it.