1.为用户设置角色

 {
Layout = null;
}
@using OA.Model
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SetRoleInfo</title>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">
function subForm()
{
$('#form1').submit();
}
function a()
{
alert("a");
}
function afterSubmit(data)
{
window.parent.AfterSubmit(data)
//parent.document.getElementsByTagName("roleFrame")["roleFrame"].AfterSubmit(data);
}
</script>
</head>
<body>
<div>
为用户@{
UserInfo userInfo = ViewBag.UserInfo;
@userInfo.UName
}添加角色
@using (Ajax.BeginForm("AddRole", "UserInfo", new { }, new AjaxOptions() { HttpMethod = "Post", OnSuccess = "afterSubmit" }, new { id = "form1" }))
{
<input type="hidden" name="userId" value="@userInfo.ID"/>
IList<RoleInfo> roleInfoList=ViewBag.RoleInfoList;
IList<int> roleIds=ViewBag.roleIds;
string ckName="ck_"; foreach (RoleInfo role in roleInfoList)
{
string checkName=ckName+role.ID;
if (roleIds.Contains(role.ID))
{
<input type="checkbox" value="@role.ID" name="@checkName" id="@role.ID" checked="checked"/>@role.RoleName<br/>
}
else
{
<input type="checkbox" value="@role.ID" name="@checkName" id="@role.ID" />@role.RoleName<br/>
} }
}
</div>
</body>
</html>

后台代码

  public ActionResult AddRole()
{
int userId = int.Parse(Request["userId"]);
string[] allKeys = Request.Form.AllKeys;
IList<int> roleIdList = new List<int>();
foreach (string k in allKeys)
{
if (k.StartsWith("ck_"))
{
string k1 = k.Replace("ck_", "");
roleIdList.Add(int.Parse(k1));
}
}
string result = userInfoService.SetRole(userId, roleIdList);
return Content(result);
}

2.为用户设置权限

 @{
Layout = null;
}
@using OA.Model
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SetUserActionInfo</title>
<link href="~/Content/themes/default/easyui.css" rel="stylesheet" />
<link href="~/Content/themes/icon.css" rel="stylesheet" />
<link href="~/Content/tableStyle.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/easyui-lang-zh_CN.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
</head>
<body>
<div>
为用户@{
UserInfo user = ViewBag.User as UserInfo;
@user.UName }分配权限
<table>
<tr>
<th>权限编号</th>
<th>权限名称</th>
<th>地址</th>
<th>请求方式</th>
<th>操作</th>
</tr> @{ IList<ActionInfo> actionInfoList = ViewBag.ActionInfoList as IList<ActionInfo>;
IList<R_UserInfo_ActionInfo> userActionList = ViewBag.UserActionList as IList<R_UserInfo_ActionInfo>;
foreach (ActionInfo action in actionInfoList)
{
<tr>
<td>@action.ID</td>
<td>@action.ActionInfoName</td>
<td>@action.Url</td>
<td>@action.HttpMethod</td>
<td>
@{
var result =( from a in userActionList
where a.ActionInfoID == action.ID
select a).ToList();
if (result != null&&result .Count >)
{
R_UserInfo_ActionInfo rua = result.FirstOrDefault();
if (rua.IsPass)
{
<label for="[email protected]">允许</label><input type="radio" class="radiosecelction" checked="checked" name="[email protected]" id="[email protected]" ids="@action.ID" value="" /> <label for="[email protected]">禁止</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" ids="@action.ID" value="" />
<input type="button" value="清除" class="clearbtn" ids="@action.ID" />
}
else
{
<label for="[email protected]">允许</label><input type="radio" name="[email protected]" class="radiosecelction" id="[email protected]" value="" ids="@action.ID" /> <label for="[email protected]">禁止</label><input type="radio" checked="checked" class="radiosecelction" name="[email protected]" id="[email protected]" value="" ids="@action.ID" />
<input type="button" value="清除" class="clearbtn" ids="@action.ID" />
}
}
else
{
<label for="[email protected]">允许</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" value="" ids="@action.ID" /> <label for="[email protected]">禁止</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" value="" ids="@action.ID" />
<input type="button" value="清除" class="clearbtn" ids="@action.ID" />
}
}
</td>
</tr>
}
}
</table>
</div>
</body>
</html>
<script type="text/javascript">
$(".radiosecelction").click(function () {
var obj=$(this);
$.post(
"/UserInfo/SetUserAction",
{ "userId": '@user.ID', "actionId": obj.attr("ids"), "isPass": obj.val() },
function (data)
{
if (data == "OK") {
$.messager.show({
title: '提示',
msg: '权限修改成功',
showType: 'show'
});
}
else {
$.messager.show({
title: '提示',
msg: '权限修改失败',
showType: 'show'
});
} } )
});
$(".clearbtn").click(function () { var obj = $(this);
$.post(
"/UserInfo/ClearUserAction",
{ "userId": '@user.ID', "actionId": obj.attr("ids") },
function (data) {
if (data == "OK") {
obj.parent().find(".radiosecelction").removeAttr("checked")
$.messager.show({
title: '提示',
msg: '权限删除成功',
showType: 'show'
});
}
else {
$.messager.show({
title: '提示',
msg: '权限修改失败',
showType: 'show'
});
} } )
});
</script>

后天代码

  public ActionResult SetActionInfo()
{
int id = Request["id"] == null ? - : int.Parse(Request["id"]);
if (id >= )
{
//需要获取所有的权限信息进行展示
//需要获取用户已经拥有的权限信息进行展示
//需要获取用户的信息,如用户的名称
UserInfo user = userInfoService.LoadEntity(u => u.ID == id).FirstOrDefault();
ViewBag.User = user;
IList<ActionInfo> actionInfoList = actionInfoService.LoadEntity(a => a.DelFlag == (short)DelFlagEnum.Noraml).ToList();
ViewBag.ActionInfoList = actionInfoList;
IList<R_UserInfo_ActionInfo> userActionList = user.R_UserInfo_ActionInfo.ToList();
ViewBag.UserActionList = userActionList;
}
return View("SetUserActionInfo");
} //为用户修改权限
public ActionResult SetUserAction()
{
int userId = int.Parse(Request["userId"]);
int actionId = int.Parse(Request["actionId"]);
bool isPass = Request["isPass"] == "" ? true : false;
string result = userInfoService.SetUserAction(userId, actionId, isPass);
return Content(result);
}
05-11 19:36