MVC+ajax权限管理

不喜欢说废话,直接了当:

1、控制器

 /// <summary>
/// 获取列表
/// </summary>
/// <returns></returns>
public ActionResult GetRoleList()
{
return View();
} public string GetListF(int id)
{
List<CM_Menu> list= _menuService.GetListF(id);
string html = "";
if (list.Count > )
{
foreach (var item in list)
{
html += "<li><button onclick=GetMenu(" + item.Id + ") class='button'>" + item.Name + "</button>";
}
}
return html;
}
/// <summary>
/// 子菜单加载(权限)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string GetListSon(int id)
{
//权限判断
CM_Info_Role roleModel = Session["InfoRole"] as CM_Info_Role;
string role = roleModel.Role;
List<CM_Menu> list= _menuService.GetListSon(id);
string html = "";
if (list.Count > )
{
for (int i = ; i < list.Count;i++ )
{
int temp = Convert.ToInt32(list[i].ShowName.Replace("role", "").Trim());
if (role.Substring(temp, ) == "")
{
html += "<li><a href='" + list[i].URL + "' target='_blank' >" + list[i].Name + "</a></li>";
}
else
{
html += "";
}
}
}
return html;
}

2、ajax

function bodyonload() //加载的是父ID为0的,即最主要的菜单
{
var data = 0;
$.ajax({
type: 'post',
async: false,
data: { id: data },
url: '/Menu/GetListF',
dataType: 'html',
success: function (json, textStatus) {
$("#MenuListF").append(json);
},
complete: function (XMLHttpRequest, textStatus) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.messager.alert("失败提示", textStatus);
}
});
//GetMenu(0);//初始化加载0的
} function GetMenu(fid)//获取
{
$.ajax({
type: 'post',
async: false,
data: { id: fid },
url: '/Menu/GetListSon',
dataType: 'html',
success: function (json, textStatus) {
//if (!$("#MenuListSon").has("li").length) {
// $("#MenuListSon").append(json);
//}
//else {
$("#MenuListSon > *").remove();
$("#MenuListSon").append(json);
//}
},
complete: function (XMLHttpRequest, textStatus) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.messager.alert("失败提示", textStatus);
}
});
}

3、页面

 <div id="head_wrap" class="container_12">
<div id="logo" class="grid_4">
<h1><span>logo</span></h1>
</div>
<div id="controlpanel" class="grid_8">
<ul>
<li><p><strong>你好,用户名</strong></p></li>
</ul>
</div>
<div id="navigation" class=" grid_12">
<ul id="MenuListF">
</ul>
</div>
</div><!-- end headwarp -->

  在大神面前来说,这就是个菜,但是加载这个菜单的时候,可能因为新手的缘故,对自己解决问题还是挺满意的。

  解释一下:

  控制器里面其实就是输出html字符串,ajax里面的就是加载一下这个字符串。页面调用显示

  

int temp = Convert.ToInt32(list[i].ShowName.Replace("role", "").Trim());
if (role.Substring(temp, ) == "")
{
html += "<li><a href='" + list[i].URL + "' target='_blank' >" + list[i].Name + "</a></li>";
}
else
{
html += "";
}

这里其实就是我的权限判断了,我觉得这样做的话后台代码也没有那么容易暴露,因为这个权限是基于 User、Menu、Role做的,我觉得这个思路就是这个样子

上一篇:Linux 桌面玩家指南:03. 针对 Gnome 3 的 Linux 桌面进行美化


下一篇:我的硬盘安装ArchLinux+xorg+gnome+美化