I am trying to post back the changes to the nested list of checkboxes for Groups and their Users but keep getting my list Count = 0 when it posts. Right now, there are no groups within groups, but I would still like to make this recursive if we move towards that in the future.
I have a hierarchical IList of GroupsUsers attached to my Activity Model as such:
Activity:
public class Activity
{
public int ActivityId { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public string Description { get; set; }
public Nullable<int> ParentId { get; set; }
public IList<GroupsUsers> Hierarchy { get; set; }
}
GroupsUsers:
public class GroupsUsers
{
public Guid? Guid { get; set; }
public string Name { get; set; }
public bool IsAllowed { get; set; } = false;
public IList<GroupsUsers> Children { get; set; } = new List<GroupsUsers>();
}
I have tried EditorFor, Partial View, and Helper but am having no luck with any of them posting back the Hierarchy. My Model.Hierarchy is posting back with Count = 0.
Here's my current attempt:
Main View (watered down):
@model MyProject.Models.Activity
@using (Html.BeginForm())
{
<!-- Activity stuff -->
<ul style="list-style:none;">
@for (var i = 0; i < Model.Hierarchy.Count(); i++)
{
@Html.EditorFor(model => model.Hierarchy[i])
}
</ul>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
Current Attempt. GroupsUsers.cshtml:
@model MyProject.Models.GroupsUsers
<li>
@Html.HiddenFor(model => model.Guid)
@Html.HiddenFor(model => model.Name)
@Html.CheckBoxFor(model => model.IsAllowed, new { @class = "groupsusers-checkbox", @style = "margin-right:5px; cursor:pointer;", @value = Model.Guid.ToString() }) @Html.LabelFor(model => model.IsAllowed, Model.Name, new { @class = "build-checkbox-label", @style = "font-weight:normal; margin-top:-2px;" })
@if (Model.Children.Any())
{
<ul style="list-style:none;">
@for (var i = 0; i < Model.Children.Count(); i++)
{
@Html.EditorFor(model => model.Children[i])
}
</ul>
}
</li>
I'm looking for my list of checkboxes to display as a list hierarchy recursively and post Model.Hierarchy back properly.
Any help would be appreciated... I only included Attempt #2 and #3 in case I was close to having it correct.