ASP.Net MVC - 处理多个复选框 [英] ASP.Net MVC - Handle Multiple Checkboxes

查看:304
本文介绍了ASP.Net MVC - 处理多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个适当的基于角色的权限系统,并希望管理员对能够编辑权限,每个角色。要做到这一点,我需要装载大量的复选框,但是我从视图

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, however I'm struggling with getting the return data from the View

请注意:我环顾四周,我也发现了类似的问题,但由于目前还找不到一个解决方案

Please Note: I have looked around, I have found similar questions but as of yet cannot 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();
%>

和控制器,

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

            return View();
        }

很显然,我需要知道哪些箱不检查,以及是的人。但是因为整个的(真,假)的返回值我不能工作,这值与该复选框。

Obviously I need to know which boxes are not checked as well as the ones that are. But in the return values because of the whole ("true,false") I cant work out which value relates to which checkbox.

以修复或$ P $有什么建议phaps另一种方法将appriciated。

Any suggestions as to a fix or prehaps an alternate method would be appriciated.

推荐答案

下面是code的一些片段,我们使用成员分配给项目,希望这可以帮助你!

Here are some snippets of code that we use to assign members to a project, hopefully this helps you out!

在我们的观点:

<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复选框助手工作时,记得是使用含有()来判断真或假的。

The main thing to remember when working with the Html Checkbox Helper is to use contains() to determine true or false.

这篇关于ASP.Net MVC - 处理多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆