Asp.net mvc 4 在视图中显示自引用模型中的子对象

Asp.net mvc 4 在视图中显示自引用模型中的子对象,asp.net-mvc-4,entity-framework-5,code-first,entity-relationship,self-reference,Asp.net Mvc 4,Entity Framework 5,Code First,Entity Relationship,Self Reference,我正在ASP.NET MVC 4上开发一个应用程序在这个应用程序中,我有一个类别和产品表, 我有一个保存类别的自参考模型: public class Category { public int Id { get; set; } public string Name { get; set; } public int? ParentId { get; set; } public virtual Category Parent

我正在ASP.NET MVC 4上开发一个应用程序在这个应用程序中,我有一个类别和产品表, 我有一个保存类别的自参考模型:

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 virtual ICollection<Product> Products { get; set; }
        public byte[] RowVersion { set; get; }
    }
部分观点:

@model IEnumerable<SarbarzDarb.Models.Entities.Category>
@foreach (var item in Model)
{
    if (item.Parent != null)
    {
    <li class="dropdown">
        @Html.ActionLink(item.Parent.Name, actionName:"Category", controllerName: "Product", routeValues: new {Id=item.Id, productName=item.Name.ToSeoUrl() }, htmlAttributes:null)


    </li>
}
}
现在我的问题是如何在局部视图中显示子对象。
我应该使用类别表中的另一列来指定父级和子级之间的关系吗?例如,在上表中,我如何找出什么是儿童2或儿童3的父母,依此类推?或者哪位家长是孩子1的家长?
以下代码是我的类别模型的配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Category>()
           .HasOptional(c => c.Parent)
           .WithMany(c => c.Children)
           .HasForeignKey(p => p.ParentId);
            base.OnModelCreating(modelBuilder);
        } 
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(c=>c.Parent)
.有许多(c=>c.儿童)
.HasForeignKey(p=>p.ParentId);
基于模型创建(modelBuilder);
} 

例如,我搜索了很多,但没有得到答案。

我的想法应该有点不同
ParentID
不应等于
id
。这毫无意义。因此,如果子级可能只有一个父级,则可以按如下方式更改表:

Id  Name        RowVersion  ParentId
--  ------      ----------  --------
1   Parent1     NULL        NULL
2   Parent2     NULL        NULL
3   Child1      NULL        1
4   child2      NULL        1
5   child3      NULL        2
现在,要获得Parent1的子级,您只需要进行如下查询。但从你的观点来看,你甚至不需要这个查询

var children = db.Categories.Where(g=>g.Id == 1);
然后您可以修改您的视图:

@foreach (var item in Model)
{

    <li class="dropdown">
        @Html.ActionLink(item.Name, "Category", "Product", new {Id=item.Id, productName=item.Name.ToSeoUrl() }, null)
        <ul>
       @foreach (var child in item.children)
        {
            <li class="dropdown">
        @Html.ActionLink(child.Name, "Category", "Product", new {Id=child.Id, productName=child.Name.ToSeoUrl() }, null)
           </li>
         }
        </ul>

    </li>
 }

我解决了我的问题,我应该以这种方式更改
CategoryService
GetAll
方法中的代码:

public IList<Category> GetAll()
        {

            return _category.Where(category => category.ParentId == null)
                            .Include(category => category.Children).ToList();
        }
public IList GetAll()
{
返回_category.Where(category=>category.ParentId==null)
.Include(category=>category.Children).ToList();
}
在我看来:

public ActionResult Categories()
        {
            var query = _categoryService.GetAll();
            return PartialView("_Categories",query);
        }
@model IEnumerable<SarbarzDarb.Models.Entities.Category>
@foreach (var item in Model)
{

    <li class="dropdown">
        @Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)

        <ul class="dropdown-menu">
            @foreach (var child in item.Children)
            {
                <li class="dropdown">
                    @Html.ActionLink(child.Name, "Category", "Product", new { Id = child.Id, productName = child.Name.ToSeoUrl() }, null)
                </li>
        }
        </ul>
    </li>

}
@model IEnumerable
@foreach(模型中的var项目)
{
  • @ActionLink(item.Name,actionName:“Category”,controllerName:“Product”,routeValue:new{Id=item.Id,productName=item.Name.toseurl()},htmlAttributes:null)
      @foreach(item.Children中的变量child) {
    • @ActionLink(child.Name,“Category”,“Product”,新{Id=child.Id,productName=child.Name.ToSeoUrl()},null)
    • }
  • }
    谢谢,那我的分类课呢?可以吗?我的意思是关于它的设计和模型创建时的配置。@Sirwanafi在我看来这是绝对正常的。对我来说,应该有用。
    ParentId  ChildID 
    --        ------      
    2          3
    1          3
    1          4
    2          5
    1          5
    
    public IList<Category> GetAll()
            {
    
                return _category.Where(category => category.ParentId == null)
                                .Include(category => category.Children).ToList();
            }
    
    @model IEnumerable<SarbarzDarb.Models.Entities.Category>
    @foreach (var item in Model)
    {
    
        <li class="dropdown">
            @Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)
    
            <ul class="dropdown-menu">
                @foreach (var child in item.Children)
                {
                    <li class="dropdown">
                        @Html.ActionLink(child.Name, "Category", "Product", new { Id = child.Id, productName = child.Name.ToSeoUrl() }, null)
                    </li>
            }
            </ul>
        </li>
    
    }