I am trying to bind the Checkboxlist with some data from an API in MVC but after checking unchecking the check boxes am posting the form to get checked options from the list but in Post action am getting the Model as null.
I am passing List<ListItem>
to View from Get action and receiving as IEnumerable<ListItem>
in view and In post action am receiving as IEnumerable<ListItem>
.
I tried to change the param to List and FormCollection only in case of FormCollection i can able to recieve model as resultsView.
How can i get the model in Post action of the controller?
Model
public class ListItemModel
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public bool Checked
{
get;
set;
}
}
EditorTemplate
@model APITestCore.Models.ListItemModel
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
@Html.CheckBoxFor(x => x.Checked)
@Html.DisplayFor(x => x.Name)
View
@model IEnumerable<APITestCore.Models.ListItemModel>
@{
ViewBag.Title = "ListTest";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>List</h2>
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input id="Submit1" type="submit" value="submit" />
}
Controller Action // POST:
[HttpPost]
public string DeviceList(IEnumerable<ListItemModel> list)
{
string response = String.Empty;
foreach(ListItemModel item in list)
{
if (item.Checked)
{
response += item.Id;
response += ",";
}
}
return response;
}