Ok, I have a role based permission system in place and would like admin\'s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, ho
Here are some snippets of code that we use to assign members to a project, hopefully this helps you out!
In the view we have:
<p>
<label>
Select project members:</label>
<ul>
<% foreach (var user in this.Model.Users)
{ %>
<li>
<%= this.Html.CheckBox("Member" + user.UserId, this.Model.Project.IsUserInMembers(user.UserId)) %><label
for="Member<%= user.UserId %>" class="inline"><%= user.Name%></label></li>
<% } %></ul>
</p>
In the controller we have:
// update project members
foreach (var key in collection.Keys)
{
if (key.ToString().StartsWith("Member"))
{
int userId = int.Parse(key.ToString().Replace("Member", ""));
if (collection[key.ToString()].Contains("true"))
this.ProjectRepository.AddMemberToProject(id, userId);
else
this.ProjectRepository.DeleteMemberFromProject(id, userId);
}
}
The main thing to remember when working with the Html Checkbox Helper is to use contains() to determine true or false.
Be sure to check this topic on SO.