开发者

How do I build a self-referencing Model Object for Hierarchical data in asp.net MVC?

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:

How do I build a self-referencing Model Object for Hierarchical data in asp.net MVC?

And here's what the association should look like:

How do I build a self-referencing Model Object for Hierarchical data in asp.net MVC?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜