MVC+Bootstrap 企业通用框架搭建--左侧导航菜单的实现(2)
为了实现点击之后导航菜单变成这个样式我使用了mvc的切面编程实现;就是每点击一个菜单进入SupportFilterAttribute 这个类的OnActionExecuting 方法中得到方法的控制器的url;去数据库查询点击了那个导航菜单
保存到导航菜单点击表信息里面;根据这个改变了导航菜单点击的样式;
SupportFilterAttribute代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using SqlServer.Entity;
using SqlServer.BLL;
using SqlServer.DAL;
using SqlServer.IDAL;
namespace ManagerSystem.UI.Filters
{
public class SupportFilterAttribute : System.Web.Mvc.ActionFilterAttribute
{
public string ActionName { get; set; }
private string Area;
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
/// <summary>
/// Action加上[SupportFilter]在执行actin之前执行以下代码,通过[SupportFilter(ActionName="Index")]指定参数
/// </summary>
/// <param name="filterContext">页面传过来的上下文</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//读取请求上下文中的Controller,Action,Id
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
RouteData routeData = routes.GetRouteData(filterContext.HttpContext);
//取出区域的控制器Action,id
string ctlName = filterContext.Controller.ToString();
string[] routeInfo = ctlName.Split(‘.‘);
string controller = null;
string action = null;
string id = null;
int iAreas = Array.IndexOf(routeInfo, "Areas");
if (iAreas > 0)
{
//取区域及控制器
Area = routeInfo[iAreas + 1];
}
int ctlIndex = Array.IndexOf(routeInfo, "Controllers");
ctlIndex++;
controller = routeInfo[ctlIndex].Replace("Controller", "");
string url = HttpContext.Current.Request.Url.ToString();
string[] urlArray = url.Split(‘/‘);
int urlCtlIndex = Array.IndexOf(urlArray, controller);
urlCtlIndex++;
if (urlArray.Count() > urlCtlIndex)
{
action = urlArray[urlCtlIndex];
}
urlCtlIndex++;
if (urlArray.Count() > urlCtlIndex)
{
id = urlArray[urlCtlIndex];
}
//url
action = string.IsNullOrEmpty(action) ? "Index" : action;
int actionIndex = action.IndexOf("?", 0);
if (actionIndex > 1)
{
action = action.Substring(0, actionIndex);
}
id = string.IsNullOrEmpty(id) ? "" : id;
//URL路径
string filePath = HttpContext.Current.Request.FilePath;
ChangeMenu(".." + filePath);
}
public void ChangeMenu(string url)
{
using (Navbar_MenuBLL nav = new Navbar_MenuBLL()
{
menu = new Navbar_MenuService()
})
{
if (!string.IsNullOrEmpty(url))
{
Navbar_Menu me = nav.SelectMenuByUrl(url);
if (me != null)
{
if (me.Parent_menu_id == 1)
{
Menu_Operation oper = new Menu_Operation();
oper.Menu_id = me.Nav_menuid;
using (Menu_OperationBLL op = new Menu_OperationBLL()
{
operation = new Menu_OperationService()
})
{
op.AddMenu_Operation(oper);
}
}
else
{
Navbar_Menu m = nav.SelectMenuByID(me.Parent_menu_id);
if (!string.IsNullOrEmpty(m.Menu_url))
{
ChangeMenu(m.Menu_url);
}
else
{
SelectNav(m.Nav_menuid);
}
}
}
}
}
}
public void SelectNav(int pid)
{
using (Navbar_MenuBLL nav1 = new Navbar_MenuBLL()
{
menu = new Navbar_MenuService()
})
{
Navbar_Menu m = nav1.SelectMenuByID(pid);
if (m.Parent_menu_id == 1)
{
Menu_Operation oper = new Menu_Operation();
oper.Menu_id = m.Nav_menuid;
using (Menu_OperationBLL op = new Menu_OperationBLL()
{
operation = new Menu_OperationService()
})
{
op.AddMenu_Operation(oper);
}
}
else
{
SelectNav(m.Parent_menu_id);
}
}
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
}
}
}
改变菜单样式的代码
$.ajax({
type: "POST",
url: "../Shared/GetALLNavbarMenu?" + Math.random().toString(),
data:{navname:""},
success: function (msg) {
$("#nav_menu").empty();
$("#nav_menu").append(msg);
$.ajax({
type: "POST",
url: "../Shared/GetMenu_Operation?" + Math.random().toString(),
success: function (msg) {
if (msg.Menu_id == "" | msg.Menu_id == null | msg.Menu_id == undefined) {
$("#nav_menu>ul>li").eq(2).removeClass("active").addClass("active");
}
$("#nav_menu>ul>li>a").append("<span class=\"selected\"></span>");
$("#nav_menu>ul>li").removeClass("active open");
$("#" + msg.Menu_id).removeClass("active open").addClass("active open");
$("#" + msg.Menu_id).children("ul").css("display", "block");
}
});
App.init();
}
});
});
function ChangeStyle(obj) {
$("#nav_menu>ul>li").removeClass("active");
var title = $(obj).parent().attr("id");
$(obj).parent().removeClass("active").addClass("active");
}
function ChangeliStyle(obj) {
$.ajax({
type: "POST",
url: "../Shared/AddMenu_Operation?" + Math.random().toString(),
data:{menu_id:$(obj).parent().attr("id")},
success: function (msg) {
}
});
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。