ساخت منوهای چند سطحی در ASP.NET MVC
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۲/۰۷/۱۹ ۱۱:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Menu.Models.Entities
{
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Children { get; set; }
}
}
public class MyContext : DbContext
{
public DbSet<Category> Category { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Self Referencing Entity
modelBuilder.Entity<Category>()
.HasOptional(x => x.Parent)
.WithMany(x => x.Children)
.HasForeignKey(x => x.ParentId)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
}
این حالت میتواند تا n سطح پیش برود، حال نحوه نمایش در View مربوطه باید به صورت زیر باشد :
@using Menu.Helper
@model IEnumerable<.Models.Entities.Category>
@ShowTree(Model)
@helper ShowTree(IEnumerable<Menu.Models.Entities.Category> categories)
{
foreach (var item in categories)
{
<li class="@(item.Children.Any() ? "dropdown-submenu" : "")">
@Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)
@if (item.Children.Any())
{
<ul>
@ShowTree(item.Children)
</ul>
}
</li>
}
} public IList<Category> GetAll()
{
//return _category.AsNoTracking().ToList();
return _category.Where(category => category.ParentId == null)
.Include(category => category.Children).ToList();
} [ChildActionOnly]
public ActionResult Categories()
{
var query = _categoryService.GetAll();
return PartialView("_Categories",query);
} .dropdown-submenu{position:relative;}
.dropdown-submenu>.dropdown-menu{top:0;left:100%;margin-top:-6px;margin-left:-1px;-webkit-border-radius:0 6px 6px 6px;-moz-border-radius:0 6px 6px 6px;border-radius:0 6px 6px 6px;}
.dropdown-submenu:hover>.dropdown-menu{display:block;}
.dropdown-submenu>a:after{display:block;content:" ";float:right;width:0;height:0;border-color:transparent;border-style:solid;border-width:5px 0 5px 5px;border-left-color:#cccccc;margin-top:5px;margin-right:-10px;}
.dropdown-submenu:hover>a:after{border-left-color:#ffffff;}
.dropdown-submenu.pull-left{float:none;}.dropdown-submenu.pull-left>.dropdown-menu{left:-100%;margin-left:10px;-webkit-border-radius:6px 0 6px 6px;-moz-border-radius:6px 0 6px 6px;border-radius:6px 0 6px 6px;} .dropdown-submenu{position:relative}
.dropdown-submenu > .dropdown-menu{top:0;right:100%;margin-top:-6px;margin-right:-1px;-webkit-border-radius:0 6px 6px 6px;-moz-border-radius:0 6px 6px;border-radius:0 6px 6px 6px}
.dropdown-submenu:hover > .dropdown-menu{display:block}
.dropdown-submenu > a:after{display:block;content:" ";float:left;width:0;height:0;border-color:transparent;border-style:solid;border-width:5px 5px 5px 0;border-right-color:#ccc;margin-top:5px;margin-left:-10px}
.dropdown-submenu:hover > a:after{border-left-color:#fff}
.dropdown-submenu.pull-Right{float:none}
.dropdown-submenu.pull-Right > .dropdown-menu{right:-100%;margin-right:10px;-webkit-border-radius:6px 0 6px 6px;-moz-border-radius:6px 0 6px 6px;border-radius:6px 0 6px 6px} [CacheMethod(SecondsToCache = ?)]
@Html.ActionLink(item.Name, actionName: "Category" , controllerName: "Product" , routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null )