I have these view models.
public class MasterFrmVm
{
public Guid Id { get; set; }
public IList<FrmVm> FrmVms { get; set; }
public MasterFrmVm()
{
RewardTierFrmVm = new List<RewardTierFrmVm>();
}
}
public class FrmVm
{
public Guid Id { get; set; }
public IList<GroupedStoreVm> GroupedStoresVm { get; set; }
public FrmVm()
{
GroupedStoresVm = new List<GroupedStoreVm>();
}
}
public class GroupedStoreVm
{
public string CountryName { get; set; }
public string CountryId { get; set; }
public IList<StoreGroupingVm> StoreGroupingVms { get; set; }
public GroupedStoreVm()
{
StoreGroupingVms = new List<StoreGroupingVm>();
}
}
public class StoreGroupingVm
{
public string Name { get; set; }
public Guid Id { get; set; }
public IList<StoreVm> StoreVms { get; set; }
}
public class StoreVm
{
public Guid Id { get; set; }
public string Name { get; set; }
}
@foreach (var f in Model.FrmVm )
{
@Html.Partial("GenerateTier", f)
}
//GenerateTier
@model FrmVm
<ul class="storeTree">
@for (int i = 0; i < Model.GroupedStoresVm.Count; i++)
{
<li>
<input type="checkbox" name="@Model.GroupedStoresVm[i]" value="@Model.GroupedStoresVm[i].CountryId" />@Model.GroupedStoresVm[i].CountryName
<ul>
@for (int x = 0; x < Model.GroupedStoresVm[i].StoreGroupingVms.Count; x++)
{
<li>
@Html.CheckBox(Model.GroupedStoresVm[i].StoreGroupingVms[x].Name , false, new { value = Model.GroupedStoresVm[i].StoreGroupingVms[x].Id }) @Model.GroupedStoresVm[i].StoreGroupingVms[x].Name
<ul>
@for (int z = 0; z < @Model.GroupedStoresVm[i].StoreGroupingVms[x].StoreVms.Count; z++)
{
<li>@Html.CheckBox(Model.GroupedStoresVm[i].StoreGroupingVms[x].StoreVms[z].Name, false)@Model.GroupedStoresVm[i].StoreGroupingVms[x].StoreVms[z].Name </li>
}
</ul>
</li>
}
</ul>
</li>
}
</ul>
I am basically making a checkbox tree (then using a jquery plugin I make it look like a tree). However I am not sure how to make the checkboxes so they bind. Normally I would use strongly typed html helper but I just don’t know how it would look as it wants a bool value.
So I tried both the non strongly typed helper and just html but I can’t figure out how to make it so when I have my controller it binds.
public ActionResult (FrmVm vm)
{
}
I am using jquery and serializing it and sending it via Ajax.
Edit
I also forgot to mention the FrmVm is another view Model as it too can also be many.
@foreach (var f in Model.FrmVm)
{
@Html.Partial("GenerateTier", f)
}
what then has the code I showed above. Now added to my code.
Edit 2
Here is what I see in firebug
FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.a=a
&FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.a=false
&FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.3=70563225-2718-40ec-8a71-a01500a66183
&FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.3=false
&FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.1=dbd43e7e-86e4-4fa1-9e48-a01a00db151c
&FrmVm%5Bd06a6c21-1796-4fb5-9af5-cbf973c6c820%5D.1=false
The structure would be (they would be checkboxes of course)
-a
- 3
-1
not sure why it says they are all false as I checked them all.
When your viewmodel has a property defined as a
boolthen the helper@Html.EditorFor(model => model.boolProperty)will automatically produce a checkbox.In response to your comment, perhaps something like this?
view