簡體   English   中英

如何在asp.net MVC中為分層數據構建自引用模型對象?

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

我試圖了解如何為層次數據構建自引用模型。 最終我將創建一個類別樹。

我還沒有表格中的任何內容。 在我將結構固定后,我將創建表。 我現有的Object定義如下:

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

我的假存儲庫填充類似這樣的類別:

// 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();

我堅持如何使這個自我引用對象,我可以發送到視圖顯示。 我在想控制器是這樣的:

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

我不知道我是否正確接近這一點。 我將使用recurses的自定義HtmlHelper方法顯示類別樹。

我如何將Categories作為自引用對象?

編輯:改述問題

我開始認為我在問我頭上的問題:-)

請檢查此代碼:

[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; }
    }
}

此代碼編譯,我不必更改我的虛假存儲庫。 我覺得我錯過了一些東西。 我不知道如何測試它是否有效。 我正在推動我的知識極限。

我甚至不確定正確的問題是什么。 我打算玩這個...看看我是否收到錯誤或者它是否按照我期望的方式運行。 我可能會再次在這里編輯。

編輯2

看來Category.category只是返回null。 並且,它似乎不在任何類型的列表中。 我不確定我是否正確建立了這種關系。

有什么想法嗎?

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; }
}

如果您在Visual Studio中使用Linq2Sql或Entity Framework設計器,則可以創建一個名為“Category”的實體(或類),然后創建一個返回自身的關聯。 設計者將自動在實體/類上創建一個集合屬性,允許您導航到子集合。

以下是您的Linq2Sql圖表的外觀:

替代文字

這就是協會的樣子: 替代文字

您的假存儲庫可以簡單地使用Linq2Sql類,如下所示: -

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 };

並且Linq2Sql將“神奇地”為您設置'Root'上的'Children'集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM