loyung

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在日常的项目中往往会需要使用页面的导航路径来记录和跳转模块,现在我们就说一下如何做一个设有子父级的导航。

实现效果如下:

这里结合bootstrap做样式来实现

第一步:编写获取当前模块的子父级节点信息,并且判断用户是否有权限Ian访问。

建立当前模块管理类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Count.Models.System
{
    public partial class AdminModuleCurrent
    {
        /// <summary>
        /// 当前请求模块的一级类ID 
        /// </summary>
        public virtual Int32 ParentID { get; set; }
        /// <summary>
        /// 当前请求模块的二级类ID 
        /// </summary>
        public virtual Int32 SecondId { get; set; }

        /// <summary>
        /// 页面的导航标题
        /// </summary>
        public virtual string PageTitle { get; set; }

        /// <summary>
        /// 页面的导航链接
        /// </summary>
        public virtual string BreadCrumbList { get; set; }
    }
}

当前访问模块的各级关系方法

       public static AdminModuleCurrent GetCurrentModuleParentIdByUser(string actionName, string controllerName, string areaName, string CurrentUserLimit)
       {
           var list =new BIModule().GetUserAllModule(CurrentUserLimit);
           if (list != null)
               return (from info in list where !string.IsNullOrEmpty(areaName) && !string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName) where (info.Module_Area.ToLower() == areaName.ToLower()) && (info.Module_Controller.ToLower() == controllerName.ToLower()) && (info.Module_Action.ToLower() == actionName.ToLower()) select new BIModule(). GetCurrentModule(info)).FirstOrDefault();
           else
               return null;
       }

       /// <summary>
       /// 获得当前请求的模块信
       /// </summary>
       /// <param name="minfo"></param>
       /// <returns></returns>
       public  AdminModuleCurrent GetCurrentModule(BIModuleInfo minfo)
       {
           var breadCrumbList = "";
           var breadCrumbList2 = "";
           var cmodel = new AdminModuleCurrent();

           if (minfo.P_ID == 0)
           {
               cmodel.ParentID = Convert.ToInt32(minfo.Module_ID);
               cmodel.SecondId = Convert.ToInt32(minfo.Module_ID);
               cmodel.PageTitle = minfo.Module_Name;
           }
           else
           {
               //判断是否还有父级
               var parentmodl =BIModuleInfoFindById(minfo.P_ID);
               var oneId = 0;
               if (parentmodl != null)
               {
                   oneId = Convert.ToInt32(parentmodl.P_ID);
                   var cmurl2 = GetMUrl(parentmodl);
                   breadCrumbList2 = "<li><a href=\"" + cmurl2 + "\">" + parentmodl.Module_Name + "</a><i class=\"fa fa-angle-right\"></i></li>";
               }

               if (oneId > 0)
               {
                   cmodel.ParentID = oneId;
                   cmodel.SecondId = Convert.ToInt32(minfo.P_ID);
                   cmodel.PageTitle = minfo.Module_Name;

                   var secondparentmodl = BIModuleInfoFindById(Convert.ToInt32(oneId));
                   if (secondparentmodl != null)
                   {
                       var cmurl1 = GetMUrl(parentmodl);
                       breadCrumbList = "<li><a href=\"" + cmurl1 + "\">" + secondparentmodl.Module_Name + "</a><i class=\"fa fa-angle-right\"></i></li>" + breadCrumbList2;
                   }
                   else
                   {
                       breadCrumbList = breadCrumbList2;
                   }
               }
               else
               {
                   cmodel.ParentID = Convert.ToInt32(minfo.P_ID);
                   cmodel.SecondId = Convert.ToInt32(minfo.Module_ID);
                   cmodel.PageTitle = minfo.Module_Name;

                   breadCrumbList = breadCrumbList2;
               }


           }

           breadCrumbList += "<li><a href=\"javascript:;\">" + minfo.Module_Name + "</a></li>";
           cmodel.BreadCrumbList = breadCrumbList;
           return cmodel;
       }

       /// <summary>
       /// 获得模块的url
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public static string GetMUrl(BIModuleInfo model)
       {
           var aurl = "";
           var siteurl = IM.Utils.Configuration.AppSettings("SiteUrl");
           if (!string.IsNullOrEmpty(model.Module_Url))
           {
               if (model.Module_Url.Contains("http://") == true)
               {
                   aurl = model.Module_Url;
               }
               else
               {
                   aurl = siteurl + model.Module_Area + "/" + model.Module_Controller + "/" + model.Module_Action;
               }
           }
           else if (!string.IsNullOrEmpty(model.Module_Area) && !string.IsNullOrEmpty(model.Module_Controller) && !string.IsNullOrEmpty(model.Module_Action))
           {
               aurl = siteurl + model.Module_Area + "/" + model.Module_Controller + "/" + model.Module_Action;
           }
           else
           {
               aurl = "javascript:;";
           }
           return aurl;
       }

在页面加载的基类中添加对当前访问控制器的验证和读取,并对当前访问前边所建的类进行实例化

/// <summary>
        /// 在执行action之前处理模块信息
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
base.OnActionExecuting(filterContext);
            CurrentAdminUser = IM.Utils.CookieHelper.GetCookie("uname");
            this.SiteUrl = IM.Utils.Configuration.AppSettings("SiteUrl");

            var actionName = filterContext.RequestContext.RouteData.Values["action"].ToString();
            var controllerName = filterContext.RequestContext.RouteData.Values["controller"].ToString();
            var areaName = filterContext.RequestContext.RouteData.DataTokens["area"].ToString();
            this.CurrentSiteUrl = IM.Utils.Configuration.AppSettings("SiteUrl") + areaName + "/" + controllerName + "/" + actionName;


            filterContext.Controller.ViewBag.ControllerName = "/" + areaName + "/" + controllerName + "/" + actionName;

            IsAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();

            var currentModuleParentId = BIModule.GetCurrentModuleParentIdByUser(actionName, controllerName, areaName, CurrentUserLimit);
            if (currentModuleParentId == null)
            {
                if (IsAjaxRequest)
                {
                    RedirectJavaScript(filterContext);
                }
                else
                {
                    var strurlerrorurl = @Url.Content("~/Error/ErrorInfo/ErrorAuth");
                    RedirectUrl(strurlerrorurl, filterContext);
                }
            }
            else
            {
                ViewBag.CurrentModuleParentId = currentModuleParentId;
            }

        }

视图页面调用

@{
    var mParentId = 0;
    var msecondid = 0;
    var PageTitle = "";
    var BreadCrumbList = "";
    if (ViewBag.CurrentModuleParentId != null)
    {
        var vModuleParentId = (Count.Models.System.AdminModuleCurrent)ViewBag.CurrentModuleParentId;
        mParentId = vModuleParentId.ParentID;
        msecondid = vModuleParentId.SecondId;
        PageTitle = vModuleParentId.PageTitle;
        BreadCrumbList = vModuleParentId.BreadCrumbList;
    }
    }
<div class="row-fluid">
                    <div class="span12">
                        <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                        <ul class="breadcrumb">
                            <li>
                                <i class="fa fa-home"></i>
                                <a href="@Url.Content("~/count")">首页</a>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            @Html.Raw(BreadCrumbList)

                            @* @Html.Partial("_MetronicBreadCrumb", BreadCrumbList)*@

                        </ul>
                        <!-- END PAGE TITLE & BREADCRUMB-->
                    </div>
                </div>

 

posted on 2017-07-13 09:20  loyung  阅读(1284)  评论(0编辑  收藏  举报