public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); List<Page> pages = Page.GetMenu(); Menu mainMenu = new Menu(Display.MainMenu, filterContext.RequestContext.RouteData.Values); foreach (Page p in pages) { MenuItem item = new MenuItem(mainMenu, p.Title, p.Url); mainMenu.Items.Add(item); } filterContext.Controller.ViewBag.MainMenu = mainMenu; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); Menu mainMenu = new Menu(Display.MainMenu, filterContext.RequestContext.RouteData.Values); mainMenu.Items = new List<MenuItem>() { new MenuItem(mainMenu, Display.Pages, "Index", "Pages"), new MenuItem(mainMenu, Display.Posts, "Index", "Posts"), new MenuItem(mainMenu, Display.Users, "Index", "Users"), }; filterContext.Controller.ViewBag.MainMenu = mainMenu; }
/// <summary> /// /// </summary> /// <param name="htmlHelper"></param> /// <param name="menu"></param> /// <returns></returns> public static MvcHtmlString Breadcrumbs(this HtmlHelper htmlHelper, Menu menu, string separator = ">>") { MenuItem item = menu.Items.FirstOrDefault(x => x.Items, x => x.IsCurrent); if (item != null) { List<KeyValuePair<string, string>> crumbs = new List<KeyValuePair<string, string>>(); UrlHelper url = new UrlHelper(htmlHelper.ViewContext.RequestContext); while (item != null) { KeyValuePair<string, string> link = new KeyValuePair<string, string>(item.Title, url.Action(item.Action, item.Controller, new RouteValueDictionary(item.RoutesValues))); crumbs.Add(link); item = item.Parent; } TagBuilder span = new TagBuilder("span"); for (int i = crumbs.Count - 1; i >= 0; i--) { TagBuilder linkTag = new TagBuilder("a"); linkTag.InnerHtml = crumbs[i].Key; linkTag.MergeAttribute("title", crumbs[i].Key); linkTag.MergeAttribute("href", crumbs[i].Value); if (i > 0) { span.InnerHtml += linkTag.ToString() + " " + separator + " "; } else { span.InnerHtml += linkTag.ToString(); } } return span.ToMvcHtmlString(); } return MvcHtmlString.Empty; }
/// <summary> /// /// </summary> /// <param name="htmlHelper"></param> /// <param name="menu"></param> /// <returns></returns> public static MvcHtmlString Menu(this HtmlHelper htmlHelper, Menu menu, string activeItemCssClass = "active") { if (menu == null || menu.Items.Count <= 0) return new MvcHtmlString(string.Empty); TagBuilder ulTag = htmlHelper.RenderMenuItems(menu.Items); if (menu.HtmlAttributes != null) { RouteValueDictionary attributes = new RouteValueDictionary(menu.HtmlAttributes); ulTag.MergeAttributes(attributes); } return ulTag.ToMvcHtmlString(); }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="title"></param> /// <param name="action"></param> /// <param name="controller"></param> /// <param name="visible"></param> /// <param name="htmlAttributes"></param> /// <param name="routeValues"></param> public MenuItem(Menu menu, string title, string action, string controller, bool visible, object htmlAttributes, object routeValues) : this(menu, null, title, action, controller, visible, htmlAttributes, routeValues) { }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="parent"></param> /// <param name="title"></param> /// <param name="action"></param> /// <param name="controller"></param> /// <param name="visible"></param> public MenuItem(Menu menu, MenuItem parent, string title, string action, string controller, bool visible) : this(menu, parent, title, action, controller, visible, null, null) { }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="parent"></param> /// <param name="title"></param> /// <param name="action"></param> /// <param name="controller"></param> public MenuItem(Menu menu, MenuItem parent, string title, string action, string controller) : this(menu, parent, title, action, controller, true, null, null) { }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="title"></param> /// <param name="url"></param> public MenuItem(Menu menu, string title, string url) { this.Menu = menu; this.Title = title; this.Url = url; }
/// Creates new instance of menu item /// </summary> /// <param name="menu">Menu this itm=em is belongs to.</param> /// <param name="parent">Parent MenuItem of this item.</param> /// <param name="title">The title of menu item.</param> /// <param name="action">The target controller action.</param> /// <param name="controller">The target controller.</param> /// <param name="visible">Set visibility of menu item while rendering.</param> /// <param name="htmlAttributes">An a html attributes of the menu item.</param> /// <param name="routeValues">An a additional route values to specify menu item link target.</param> public MenuItem(Menu menu, MenuItem parent, string title, string action, string controller, bool visible, IDictionary<string, object> htmlAttributes, IDictionary<string, object> routeValues) { this.Menu = menu; this.Parent = parent; this.Title = title; this.Action = action; this.Controller = controller; this.HtmlAttributes = htmlAttributes; this.RoutesValues = routeValues; this.Visible = visible; this.Items = new List<MenuItem>(); }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="parent"></param> /// <param name="title"></param> /// <param name="action"></param> /// <param name="controller"></param> /// <param name="visible"></param> /// <param name="htmlAttributes"></param> /// <param name="routeValues"></param> public MenuItem(Menu menu, MenuItem parent, string title, string action, string controller, bool visible, object htmlAttributes, object routeValues) : this(menu, parent, title, action, controller, visible, null, null) { this.HtmlAttributes = htmlAttributes.ToDictionary(); this.RoutesValues = routeValues.ToDictionary(); }