我如何建立在asp.net MVC分层数据自参照模型对象? [英] How do I build a self-referencing Model Object for Hierarchical data in asp.net MVC?

查看:215
本文介绍了我如何建立在asp.net MVC分层数据自参照模型对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何建立的层次化数据自参考模型。最终我将创建一个目录树。

我不表任何事情。我已经结构确定下来后,那么我将创建表。我现有的对象的定义如下:

 公共类分类
{
    公众的Int64 CATID {搞定;组; }
    公众的Int64? parentCatID {搞定;组; }
    公共字符串catName {搞定;组; }
    公共字符串catDescription {搞定;组; }
}

我的假库填充类别是这样的:

  //假硬codeD类别列表
私有静态的IQueryable<类别> fakeCategories =新的List<类别> {
    新的分类{CATID = 1,parentCatID = NULL,catName =根,catDescription =},
    新的分类{CATID = 2,parentCatID = 1,catName =类别W /潜艇,catDescription =},
    新的分类{CATID = 3,parentCatID = 1,catName =分类没有潜艇,但现在有潜艇,catDescription =},
    新的分类{CATID = 4,parentCatID = 2,catName =ZUB猫,catDescription =},
    新的分类{CATID = 5,parentCatID = 2,catName =子猫,catDescription =},
    新的分类{CATID = 6,parentCatID = NULL,catName =另一根,catDescription =},
    新的分类{CATID = 7,parentCatID = NULL,catName =泽德国根,catDescription =},
    新的分类{CATID = 8,parentCatID = 3,catName =全新猫,catDescription =},
    新的分类{CATID = 9,parentCatID = 8,catName =全新猫子,catDescription =},
} .AsQueryable();

我停留在如何使这个自参考对象,我可以发送到用于显示的视图。我在想,该控制器将是这样的:

 公众的ActionResult CategoryTree()
{
    变种猫= fakeRepository.Categories.ToList();
    返回查看(猫);
}

我不知道如果我正确地接近这一点。我将使用递归的自定义的HtmlHelper方法显示类别树。

我该如何获得类别来做个自我引用的对象?

编辑:改写的问题

我开始觉得我问那些在我头上的问题: - )

请检查该code:

  [表]
公共类目录
{
    [列(IsPrimaryKey = TRUE,IsDbGenerated = true时,自动同步= AutoSync.OnInsert)
    公众的Int64 CATID {搞定;组; }
    公众的Int64? parentCatID {搞定;组; }
    公共字符串catName {搞定;组; }
    公共字符串catDescription {搞定;组; }    内部的EntityRef<类别及GT; _类别;
    [公会(ThisKey =parentCatID,寄存=_category)]
    公共类别类别{
        {返回_category.Entity; }
        内部集合{_category.Entity =价值; }
    }
}

这code编译和我没有改变我的假库。我觉得像即时通讯失去了一些东西,但。我不知道如何测试这个,看看它是否工作。我推着我的知识范围。

我敢肯定,正确的问题是什么,甚至不100%。我要去玩这个......看看我得到的错误或者工作我所期望的方式。我可能会重新编辑在这里。

编辑2

看来, Category.category 刚刚返回null。并且,它似乎并不以任何类型的列表。我不知道如果我设置了正确的关系。

有什么想法?


解决方案

 公共类范畴//类的一个实例就是一类
{
    公众的Int64标识{搞定;组; }
    公共范畴家长{搞定;组; } //父链接,EF将使用协处理这个你
    公开名单<类别及GT;儿童{搞定;设置;} //当你移动到EF或L2S替换此
    公共字符串名称{;组; }
    公共字符串描述{搞定;组; }
}

如果您在Visual Studio中使用LINQ2SQL或实体框架设计,而不是你可以创建一个名为类别,然后关联回到本身就是一个实体(或类)。无论是设计师将在实体/类,允许您导航到孩子们的集合自动创建一个集合属性。

这是你的LINQ2SQL图可能是什么样子:

和这里的协会应该是什么样子:

您假货仓库可以那么简单地使用LINQ2SQL类像这样: -

 类别根=新类别(){名称=根,家长= NULL};
类别child1 =新类别(){名称=1儿童,父母=根};
类别的child2 =新类别(){名称=2孩子,家长=根};

和你LINQ2SQL会'神奇'设置'孩子'的收集上根。

I'm trying to understand how to build a self referencing model for hierachical data. Eventually i will be creating a category tree.

I don't anything in tables yet. After I have the structure nailed down then I will create the tables. My existing Object is defined like this:

public class Categories
{
    public Int64 catID { get; set; }
    public Int64? parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }
}

My fake repository populates Categories like this:

// Fake hardcoded list of categories
private static IQueryable<Categories> fakeCategories = new List<Categories> {
    new Categories { catID = 1, parentCatID = null, catName = "Root", catDescription = "" },
    new Categories { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
    new Categories { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
    new Categories { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
    new Categories { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
    new Categories { catID = 6, parentCatID = null, catName = "Another Root", catDescription = "" },
    new Categories { catID = 7, parentCatID = null, catName = "Ze German Root", catDescription = "" },
    new Categories { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
    new Categories { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();

I am stuck on how to make this a self referencing object which I can send to a view for display. I was thinking that the controller would be something like this:

public ActionResult CategoryTree()
{
    var cats = fakeRepository.Categories.ToList();
    return View(cats);
}

I don't know if I'm approaching this correctly. I would display the category tree using a custom HtmlHelper method that recurses.

How would I get Categories to be a self referencing object?

Edit: rephrased question

I'm starting to think that I'm asking questions that are over my head :-)

Please examine this code:

[Table]
public class Category
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public Int64 catID { get; set; }
    public Int64? parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }

    internal EntityRef<Category> _category;
    [Association(ThisKey = "parentCatID", Storage = "_category")]
    public Category category {
        get { return _category.Entity; }
        internal set { _category.Entity = value; }
    }
}

This code compiles and I don't have to change my fake repository. I feel like im missing something though. I'm not sure how to test this to see if it works. I'm pushing against the limits of my knowledge.

I'm not even 100% sure of what the correct question is. I'm going to go play with this... see if I get errors or if it works the way i expect. I'll probably edit here again.

Edit 2

It appears that Category.category is just returning null. And, it doesn't seem to be in a list of any type. I'm not sure if I set up the relationship correctly.

Any thoughts?

解决方案

public class Category   // an instance of the class is just ONE category
{
    public Int64 Id { get; set; }
    public Category Parent { get; set; }        // A parent link, EF will  handle this for you using an Association
    public List<Category> Children {get; set;}  // Replace this when you move to EF or L2S
    public string Name { get; set; }
    public string Description { get; set; }
}

If you use the Linq2Sql or Entity Framework designer in Visual Studio instead you can create an Entity (or Class) called 'Category' and then an association back to itself. Either designer will automatically create a collection property on the entity/class that allows you to navigate to the collection of children.

Here's what your Linq2Sql diagram might look like:

And here's what the association should look like:

Your fake repository can then simply use the Linq2Sql classes like so:-

Category root = new Category() { Name = "Root", Parent = null };
Category child1 = new Category() { Name = "Child 1", Parent = root };
Category child2 = new Category() { Name = "Child 2", Parent = root };

And Linq2Sql will 'magically' set the 'Children' collection on 'Root' for you.

这篇关于我如何建立在asp.net MVC分层数据自参照模型对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆