如何使用“编辑”页面中的选中复选框显示已存在的权限

时间:2014-12-09 13:37:15

标签: asp.net-mvc-4 checkbox asp.net-mvc-viewmodel

我必须显示附加到角色的权限列表。使用复选框创建权限附加到该角色的角色时,应在编辑页面中显示。为此,我创建了一个viewmodel。当我调用编辑页面时,权限表权限的所有复选框都处于检查模式。我有三个表角色,权限和roledetail检查的权限来自roledetail表。请帮助我做错了。 我的表格如下 作用

RoleId int,
RoleName varchar(25),
[Description] varchar(100),
Deleted bit

权限

PermissionId int,
PermissionName varchar(25)

RoleDeatil

RoleDetailId int,
RoleId int,
PermissionId int
AddedOn datetime

这是我的视图模型

public class PermissionVM
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }

编辑控制器

[HttpGet]
        public ActionResult RoleEdit(int id, ViewModelRole viewModelRole)
        {
            Role role = roleService.GetRole(id);
            viewModelRole.Role = role;
            PopulateAssignedPermissionData(role);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(viewModelRole);
        }
        private void PopulateAssignedPermissionData(Role role)
        {
            var allPermission = tDbContext.Permissions;
            var rolePermissions = new HashSet<int>(tDbContext.Permissions.Select(p => p.PermissionId));
            var viewModel = new List<PermissionVM>();
            foreach (var permission in allPermission)
            {
                viewModel.Add(new PermissionVM
                {
                    ID = permission.PermissionId,
                    Name = permission.PermissionName,
                    IsSelected = rolePermissions.Contains(permission.PermissionId)
                });
            }
            ViewBag.Permissions = viewModel;
        }

查看

@model light.ViewModels.ViewModelRole
 @{
         List<light.ViewModels.PermissionVM> permissions = ViewBag.Permissions;
          foreach (var permission in permissions)
          {
              <div class="row">
               <div class="col-md-1 col-sm-1">
               <input type="checkbox" 
                      name="tags" 
                      class="no-margin"
                      id="=ids" value="@permission.ID" 
                      @(Html.Raw(permission.IsSelected ? "checked=\"checked\"" : "")) />
                </div>
                <div class="col-md-11 col-sm-11">
                       @permission.Name
                </div>
              </div>
          }
       }

注意=在角色创建RoleId的创建时间和附加的PermissionId存储在Roledetail表中,我必须在我的页面中显示所有权限,但是应该检查在roledetail表中存在的那些复选框。请帮忙......我被困了几个小时。

2 个答案:

答案 0 :(得分:1)

我认为您的权限应该由角色过滤。像这样:

var rolePermissions = new HashSet<int>(tDbContext.RoleDetail.Where(rd => rd.RoleId == role.RoleID).Select(rd => rd.PermissionId));

您将角色传递给您的函数但是甚至不使用它:PopulateAssignedPermissionData(角色角色)。如果我理解正确的代码,现在allPermissionrolePermissions是相同的。

答案 1 :(得分:1)

尝试编辑控制器,休息很好

[HttpGet]
        public ActionResult RoleEdit(int id, ViewModelRole viewModelRole)
        {
            viewModelRole.Role = roleService.GetRole(id);
            var allPermission = tDbContext.Permissions;
            var rolePermissions = (from p in preFlightDbContext.Permissions
                                   join rd in tDbContext.RoleDetails on p.PermissionId equals rd.PermissionId
                                   where rd.RoleId == id
                                   select p.PermissionId).Distinct();
            var viewModel = new List<PermissionVM>();
            foreach (var permission in allPermission)
            {
                viewModel.Add(new PermissionVM
                {
                    ID = permission.PermissionId,
                    Name = permission.PermissionName,
                    IsSelected = rolePermissions.Contains(permission.PermissionId)
                });
            }
            ViewBag.Permissions = viewModel;
            return View(viewModelRole);
        }