How to  show multiple  checkbox on asp.Net mvc website and get value of checkbox in action method of post request of mvc controller using model property 

//METHOD FOR GET VALUES FROM DB

public List<AccessRight> getAcessRights()
        {
            return _entities.AccessRights.Where(t => t.IsActive == true).ToList();
        }

        public RoleAcessRight getRoleAcessRights(int id)
        {
             var s=_entities.RoleMasters.Where(t => t.IsActive == true && t.RoleID==id).FirstOrDefault();
            RoleAcessRight acessRight = new RoleAcessRight()
            {
                AccessRights = s.DefaultAccessRights.Split(',').Select(int.Parse).ToList(),
                ID = s.RoleID
            };
            return acessRight;
        }
     

// model class

Public class mymodelModel
{
public List<string> AccessRight { set; get; }
}

//View of action method

@{
    if (ViewBag.AccessRightList != null)
    {
        var AccessRightList = (List<AccessRight>)ViewBag.AccessRightList;
        var RoleAccessRightList = (RoleAcessRight)ViewBag.RoleAccessRightList;

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-6">
                @{
               
                    for (int i = 0; i < AccessRightList.Count(); i++)
                    {
                        var flag = RoleAccessRightList.AccessRights.Contains(AccessRightList[i].AccessRightsID);
                        <label class="checkbox-inline">
                            <input type="checkbox" Name="CreateProfileUser.AccessRight" @(flag == true ? "checked" : "" ) value="@AccessRightList[i].AccessRightsID" />@AccessRightList[i].Description
                        </label>


                    }
                }
            </div>
        </div>
    }
}

//action method Get request

 public ActionResult CreateUser(mymodelModel user)
 {
var lsttadig = _globalFunction.getRoleAcessRights(type);
            ViewBag.RoleAccessRightList = lsttadig;
            ViewBag.AccessRightList = _globalFunction.getAcessRights();
            return PartialView("GetRoleAcessRights");
}

//post request action method

[HttpPost]
        public ActionResult CreateUser(mymodelModel user)
        {
            ViewBag.operatorList = _globalFunction.getSpOperator((int)ProfileValue.Group);
            ViewBag.RoleList = new SelectList(_globalFunction.getRoles(), "ID", "Name");
            if (!ModelState.IsValid)
            {
             
                return View(user);
            }
            else
             
            return Json(new { Data = user }, JsonRequestBehavior.AllowGet);
        }

No comments:

Post a Comment

Thank You For Your Great Contribution

Featured Post

how to find n number of nodes have child or cildren

 how to find n number of nodes have child or cildren for that we use recursive function  const   data = {     'id' : '0' ,...

Popular Posts