製品をカテゴリ別にフィルタリングし、各カテゴリへのリンクを提供する必要があります。
これは StoreController のメソッドです。
public ViewResult Content(string category = null, int page = 1)
{
var model = new StoreContentViewModel
{
Items = _itemsRepository.GetItems()
.Where(i => i.Product.Category == category || i.Product.Category == null)
.OrderBy(i => i.ItemId)
.Skip((page-1)*PageSize)
.Take(PageSize),
PageInfo = new PageInfo
{
TotalItems = category == null ? _itemsRepository.GetItems().Count() :
_itemsRepository.GetItems().Where(i => i.Product.Category == category).Count(),
CurrentPage = page,
ItemsPerPage = PageSize
},
CurrentCategory = category
};
return View(model);
}
そして、NavigationController のメソッドは次のとおりです。
public PartialViewResult Menu(string category)
{
ViewBag.SelectedCategory = category;
IEnumerable<string> result = _itemsRepository.GetItems()
.Select(i => i.Product.Category)
.Distinct()
.OrderBy(i => i);
return PartialView(result);
}
このメニュー メソッドの部分ビュー:
@model IEnumerable<string>
@Html.ActionLink("All products", "Content", "Store")
@foreach (var link in Model)
{
@Html.RouteLink(link,
new
{
controller = "Navigation",
action = "Menu",
category = link,
page = 1
},
new
{
@class = link == ViewBag.SelectedCatgory ? "selectedLink" : null
}
)
}
私のモデルでは、1 つのアイテムに 1 つの製品が含まれています (ProductId は Items テーブルの外部キーです)。アプリを実行すると、「値を null または空にすることはできません。パラメーター名: controllerName」というエラーが表示されます。このアクション メソッドの単体テストも失敗します。
カテゴリ フィルタリングを追加しなくても、すべてが機能します。
問題は、_itemsRepository から「Category」プロパティを取得する行にあると思います (「filter」単体テストも失敗するため)。
_itemsRepository.Product.Category
そうですか?それが知りたいのであれば、「Category」プロパティにアクセスする他の方法はありますか??
前もって感謝します。
編集:
間違ったルーティングが原因でメッセージ エラーが発生しました。
アイテムはまだカテゴリで選択できません。問題は間違いなく次の行に関連しています:
Items = _itemsRepository.GetItems()
.Where(i => i.Product.Category == category || i.Product.Category == null)