I am trying to generate Two sets of List of checkboxes on a view. It all working apart from Post action. On submit, ParentViewModel is not binding the ChildViewModel properly Model. FirstCheckboxList Model. SecondCheckboxList Above both are coming as null.
I am not sure what I am missing. Any help on this would be great. Thanks in advance.
CheckboxItems.cshtml
@model List<CheckboxItem>
@{
for (int i = 0; i < Model.Count(); i++)
{
<div>
@Html.CheckBoxFor(x => x.ElementAt(i).Checked, new { @id = Model.ElementAt(i).Id, onclick = "GetValue()" })
<span id="Padded">@Model.ElementAt(i).Text</span>
</div>
}
}
MainView.cshtml
@Html.BeginForm(){
@Html.EditorFor(m=> m.FirstCheckboxList,"CheckboxItems")
@Html.EditorFor(m=> m.SecondCheckboxList, "CheckboxItems")
}
@Html.TextBoxFor(m => m.FSelected, new Dictionary<string,object>() {{"readonly",true}})
@Html.TextBoxFor(m => m.FUniverse,new Dictionary<string,object>() {{"readonly",true}})
<input type="submit" name="nextBtn" value ="Next" />
}
ParentViewModel
public class ParentViewModel
{
public int PId { get; set; }
public IEnumerable<CheckboxItem> FirstCheckboxList{ get; set; }
public IEnumerable<CheckboxItem> SecondCheckboxList{ get; set; }
public Int64 FSelected { get; set; }
public Int64 FUniverse { get; set; }
}
CheckboxItem : child view model
public class CheckboxItem
{
public int Id { get; set; }
public string Text { get; set; }
public bool Checked { get; set; }
}
controller action
[HttpPost]
public ActionResult MyCheckboxView(int planid, ParentViewModel model, string nextBtn)
{
// do something
return View(Model);
}