public static MvcHtmlString GetParentNavigation(this HtmlHelper helper, WebMenu menu) { if (menu == null || !menu.ParentId.HasValue) return null; StringBuilder sb = new StringBuilder(); MenuTree parent; int menuId = menu.ParentId.Value; using (var db = new WebPagesContext()) { do { parent = db.MenuTrees.First(o => o.MenuId == menuId); sb.Insert(0," > " + helper.ActionLink(parent.MenuName, "Search", new { ParentID = parent.MenuId })); menuId = parent.ParentId??0; } while (parent.ParentId.HasValue); } sb.Insert(0, helper.ActionLink("Root", "Search").ToString()); return MvcHtmlString.Create(sb.ToString()); }
// // GET: /Menu/ public ActionResult Index(int p = 1,int? parentID=null, int pageSize = 10, string orderby = "MenuId", bool desc = false) { WebMenu m = new WebMenu(); m.ParentId = parentID; m.CurrentPageNo = p; m.StartPageNo = 1; m.NeedToShow = 10; if (p > 0) p--; Expression<Func<MenuTree, bool>> where ; if (parentID.HasValue) where = o => o.ParentId == parentID.Value; else where = o => o.ParentId == null; m.Menus = db.MenuTrees .Where(where) .OrderBy(orderby, desc).Skip(pageSize * p).Take(pageSize).ToList(); m.TotalPageNo = CountTotalPage(db.MenuTrees.Where(where).Count(), pageSize); return View(m); }
public ActionResult Search(string name = "", string Url="", int? parentID = null, int p = 1, int pageSize = 10,string orderby="MenuId",bool desc=false) { WebMenu m = new WebMenu(); m.ParentId = parentID; m.CurrentPageNo = p; m.StartPageNo = 1; m.NeedToShow = 10; if (p > 0) p--; Expression<Func<MenuTree, bool>> where; if (parentID.HasValue) where = o => o.ParentId == parentID.Value; else where = o => o.ParentId == null; if (!string.IsNullOrEmpty(name)) { where =where.And(o => o.MenuName.Contains(name)); } if (!string.IsNullOrEmpty(Url)) { where = where.And(o => o.PageUrl.Contains(Url)); } var query = db.MenuTrees.AsExpandable().Where(where); m.TotalPageNo = CountTotalPage(query.Count(), pageSize); m.Menus = query.OrderBy(orderby, desc).Skip(pageSize * p).Take(pageSize).ToList(); return View(m); }