Exemple #1
0
        /// <summary>
        /// Generate Breadcrumb Navigation
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string BuildBreadcrumbNavigation(this System.Web.Mvc.HtmlHelper helper)
        {
            var menulist   = (List <GolfCentraAdmin.ViewModel.PageViewModel>)HttpContext.Current.Session["NavigationMenu"];
            var action     = helper.ViewContext.RouteData.Values["action"].ToString();
            var controller = helper.ViewContext.RouteData.Values["controller"].ToString();
            var menuitem   = menulist.Find(x => x.ActionName.ToLower() == action.ToLower() && x.ControllerName.ToLower() == controller.ToLower());

            // optional condition: I didn't wanted it to show on  account controller
            if (controller.ToLower() == "account" || menuitem == null)
            {
                return(string.Empty);
            }
            StringBuilder breadcrumb = new StringBuilder("<ol class='breadcrumb'><li>").Append(helper.ActionLink("Dashboard", "Index", "Dashboard", new { }, new { target = "_self" }).ToHtmlString()).Append("</li>");

            if (menuitem != null && menuitem.ParentId == 0)
            {
                breadcrumb.Append("<li>");
                breadcrumb.Append(helper.ActionLink(menuitem.PageName.Titleize(),
                                                    action,
                                                    controller, new { }, new { target = "_self" }));
                breadcrumb.Append("</li>");
            }
            else
            {
                var item = menulist.Find(x => x.PageId == menuitem.ParentId);
                breadcrumb.Append("<li>");
                breadcrumb.Append(item.PageName);
                breadcrumb.Append("</li>");
                breadcrumb.Append("<li>");
                breadcrumb.Append(helper.ActionLink(menuitem.PageName.Titleize(),
                                                    action,
                                                    controller, new { }, new { target = "_self" }));
                breadcrumb.Append("</li>");
            }
            return(breadcrumb.Append("</ol>").ToString());
        }
        /// <summary>
        /// Creates 'li' HTML tags with contextual styling based on the actual request.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText">Text to be displayed.</param>
        /// <param name="actionName">Action to point to.</param>
        /// <param name="controllerName">Controller to point to.</param>
        /// <returns>Returns a HTML 'li' string with the given context.</returns>
        public static MvcHtmlString MenuLink(this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
        {
            // Get the Action and the Controller for the actual request.
            var currentAction     = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
            var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

            // Creates the tag.
            var li = new TagBuilder("li");

            // Creates a <a> inside the <li> tag, pointing to the given action and controller.
            li.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString();

            // Checks if the user's actual page is the point page at the <li>. If it is, put an 'active' css class to it for contextual displaying.
            // Turns all strings to lower casing to evade casing issues not setting the 'active' css correctly.
            if (controllerName.ToLower() == currentController.ToLower() && actionName.ToLower() == currentAction.ToLower())
            {
                li.AddCssClass("active");
            }

            return(new MvcHtmlString(li.ToString()));
        }