I have a list of model like this
public class Group
{
public string Name { get; set; }
public string GroupID { get; set; }
}
In controller:
public ActionResult Index()
{
return View(ListGroup);
}
[HttpPost]
public ActionResult Index(List<Group> listModel) {
@ViewBag.Success = "Update Suceess";
return View(listModel);
}
[HttpPost]
public ActionResult Search(Group ModelSearch) {
List<Group> listResult = ListGroup.Where(m=>m.GroupID == ModelSearch.GroupID).ToList();
if (ModelSearch.GroupID == null) return View("Index", ListGroup);
return View("Index", listResult);
}
In view i group the list as below:
@foreach (var items in Model.GroupBy(m => m.GroupID).Select(g => g.ToList()))
{
<tr>
<td colspan="2">@items.ElementAt(0).GroupID</td>
</tr>
foreach (var item in items)
{
<tr>
<td>@Html.TextBoxFor(m => item.GroupID)</td>
<td>@Html.TextBoxFor(m => item.Name)</td>
</tr>
}
}
It can display as groups well but i cannot get value back to controller after button submit was fired. How can i bind model to view that controller can understand and receive data from view.
foreachloop - it generates elements with duplicatenameattributes which cannot be bound to a collection (does not have the necessary indexers). It also creates invalid html because of the duplicateidattributes. You need to use aforloop of a customEditorTemplatefor the type. - user3559349forloop and a customEditorTemplate- user3559349public ActionResult Index(List<Group> listModel)its not going to work anyway. You will need to create a view model to represent want you want to display/edit in the view. - user3559349