ASP.Net MVC - handle multiple flags

Well, I have a role-based permission system, and the administrator should be able to edit permissions for each role. To do this, I need to load a lot of checkboxes, however I'm struggling with getting the returned data from the view

Please note: I looked around, I found similar questions, but so far I can not find a solution.

 <%
     Html.BeginForm();

    string lastGroup = "";
    foreach (var CurPermission in Model)
    {

%>
        <%=Html.CheckBox("Permissions", CurPermission.Checked, new { ID = CurPermission.PermissionId}) + " " + CurPermission.PermissionValue%> 

        <br />
<%
    } 
    %>
        <input type="submit" value="Submit" />
    <%
    Html.EndForm();
%>

and controller

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditPermissions(String[] Permissions)
        {
            foreach (var CurPermission in Permissions)
            {
                Debug.WriteLine(CurPermission);
            }

            return View();
        }

Obviously, I need to know which fields are not checked, as well as those that are. But in the return values, because of the integer ("true, false"), I cannot determine which value refers to which flag.

Any suggestions for a fix or proposed alternative method will be assigned.

+5
2

, , , !

:

<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>

:

// 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);
    }
}

, , Html Checkbox Helper contains() true false.

+11

SO.

0

All Articles