为用户添加角色

subForm()定义此方法在主窗体调用
<input type="hidden" name="userId" value="@userInfo.ID" /> 定义隐藏域保存ID
@{
    Layout = null;
}
@using CZBK.ItcastOA.Model
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>为用户分配角色</title>
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        function subForm() {
            $("#form1").submit();
        }
        function afterSet(data) {
            window.parent.setUserRoleAfter(data);
        }
    </script>
</head>
<body>
    <div>
    为用户 @{
        UserInfo userInfo = ViewBag.UserInfo;
        @userInfo.UName
    } 分配角色   
        @using (Ajax.BeginForm("SetUserRole", "UserInfo", new { }, new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterSet" }, new { id = "form1" }))
        {
            <input type="hidden" name="userId" value="@userInfo.ID" />
            List<RoleInfo>roleInfoList=ViewBag.AllRoleInfo;
            List<int> roleInfoIdList = ViewBag.AllExtRoleId;
            foreach (RoleInfo roleInfo in roleInfoList)
            {
                var checkName="cba_"+roleInfo.ID;
                if (roleInfoIdList.Contains(roleInfo.ID))
                {
                    <input type="checkbox" name="@checkName" value="@roleInfo.ID" checked="checked" />@roleInfo.RoleName
                }
                else
                {
                      <input type="checkbox" name="@checkName" value="@roleInfo.ID" />@roleInfo.RoleName
                }
            }
        }
    </div>
</body>
</html>

主窗体调子窗体方法

handler: function () {
// $("#addForm").submit();//提交表单
var childWindow = $("#setRoleFrame")[0].contentWindow;
childWindow.subForm();
}

       #region 为用户分配角色
        public ActionResult SetUserRoleInfo()
        {
            int userId = int.Parse(Request["userId"]);
           UserInfo userInfo=UserInfoService.LoadEntities(u=>u.ID==userId).FirstOrDefault();
           ViewBag.UserInfo = userInfo;
            //查询所有的角色信息
           short delFlag = (short)DelFlagEnum.Normarl;
           var roleInfoList = RoleInfoService.LoadEntities(r=>r.DelFlag==delFlag).ToList();
            //找出用户已经有的角色的编号
           var userRoleIdList = (from r in userInfo.RoleInfo
                                 select r.ID).ToList();
           ViewBag.AllRoleInfo = roleInfoList;
           ViewBag.AllExtRoleId = userRoleIdList;
           return View();
        }
        /// <summary>
        /// 完成对用户角色的分配
        /// </summary>
        /// <returns></returns>
        public ActionResult SetUserRole()
        {
            int userId = int.Parse(Request["userId"]);
            string[]Allkeys=Request.Form.AllKeys;//获取表单中所有的表单元素的name属性的值。
            List<int> list = new List<int>();
            foreach (string key in Allkeys)
            {
                if (key.StartsWith("cba_"))
                {
                   string k= key.Replace("cba_","");
                   list.Add(int.Parse(k));
                }
            }
            if (UserInfoService.SetUserOrderInfo(userId, list))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }

先删除已经存在的,在增加新选择的角色。

   public bool SetUserOrderInfo(int userId, List<int> roleIdList)
        {
            var userInfo = this.GetCurrentDbSession.UserInfoDal.LoadEntities(u => u.ID == userId).FirstOrDefault();//获取用户
            if (userInfo != null)
            {
                //删除用户已有的角色.
                userInfo.RoleInfo.Clear();
                foreach (int roleId in roleIdList)
                {
                  var roleInfo=  this.GetCurrentDbSession.RoleInfoDal.LoadEntities(r => r.ID == roleId).FirstOrDefault();
                  userInfo.RoleInfo.Add(roleInfo);//给用户分配角色,通过导航属性给中间表插入数据
                }
               return this.GetCurrentDbSession.SaveChanges();
            }
            else
            {
                return false;
            }
        }

 

//为用户分配完角色以后调用该方法。
function setUserRoleAfter(data) {
if (data == "ok") {
$('#setRoleDiv').dialog('close');
} else {

}
}

posted @ 2016-12-13 15:51  ecollab  阅读(1331)  评论(0编辑  收藏  举报