ツリー構造の商品カテゴリビューを作成したいと思います。私は2011年からthis article by Ole Michelsenを見つけました。私は有望そうだと思います。この記事では、Razorを使用してツリー構造を再帰的にレンダリングするという、ほとんど覚えていたのと同様の方法について説明します。Asp.Net Core 2.0の再帰
私のデータベーステーブルは、記事で説明したものとほぼ同じです:
[ProductCategory]
Id
ParentId
Title
私は私の見解モデルがどのように見えるかわかりません。 ProductCategoryのIEnumerable
であるか、ProductCategoryのインスタンスでProductCategoryのIEnumerable
を持つ必要がありますか?このような
このような何か...
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public int NumOfProducts { get; set; }
}
...以上?
public class ViewModelProductCategory
{
public IEnumerable<ProductCategory> ProductCategory { get; set; }
//public int NumOfProducts { get; set; }//<--Not too sure about this property
public IEnumerable<Product> Products { get; set; }
}
もう一つはどのように私はSORTORDERとNumOfProductsを(現在のカテゴリ内にあるどのように多くの製品)を持つことができ、「正しい」ものに近いですか?
私が最も混乱していることは、かみそりのレンダリングがどのように一緒に来るかです。ビューモデルを再帰的な部分ビューにどのように送って、どの部分をレンダリングすべきかを部分ビューがどのように知っていますか?
私はいくつかの剃刀コードを持っていますが、私が達成しようとしているものは何も明確にしません。
この擬似コードは、おそらくより記述されています
_recursivePartial.cshtml
@model [my unknown viewmodel goes here, is it IEnumerable or not?]
<ul>
foreach item in model
{
<li>@Html.Partial("_recursivePartial.cshtml", [what goes here?])
// 1: How does _recursivePartial.cshtml know where in the tree it is?
// 2: How does this structure relate to the data structure in the DB?
}
</ul>
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components - 私は同じ使命にあります –