Search code examples
asp.net-mvc-4checkboxasp.net-mvc-viewmodel

how to show already existed permission using checked checkbox in Edit page


i have to show a list of permissions attached to role. while creating the role which permissions are attached to that role using checkbox should be displayed checked in edit page. for this i have created a viewmodel. when i call the edit page all the checkboxes for permissions of permission table are in checked mode. i have three table role,permission and roledetail the checked permissions are coming from roledetail table. please help where i am doing wrong. my tables are as follows Role

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

Permission

PermissionId int,
PermissionName varchar(25)

RoleDeatil

RoleDetailId int,
RoleId int,
PermissionId int
AddedOn datetime

here is my view Model

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

Edit Controller

[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;
        }

View

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

Note= in creation time when role is creating the RoleId and attached PermissionId's are storing in Roledetail Table and i have to show all permission in my page but ony those checkboxes should be checked which are present in roledetail table. please help.. i am stuck for hours.


Solution

  • try this for Edit Controller , and rests are Fine

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