私のコントローラーは次のようになります。
var _engine = new NopEngine();
var categoryService = _engine.Resolve<ICategoryService>();
var allCategory = categoryService.GetAllCategories();
List<string> ConvertedList = new List<string>();
for (int i = 0; i < allCategory.Count; i++)
{
ConvertedList.Add(allCategory[i].Name);
}
//fill the viewbag
ViewBag.CategoryList = ConvertedList;
return View("Nop.Plugin.Misc.ExportAttributes.Views.MiscExportAttributes.ExportCalculationSheet");
したがって、基本的には ViewBag に文字列のリストを入力しています。
私の見解は次のようになります。
@{
Layout = "";
}
@using Telerik.Web.Mvc.UI;
@model ExportCalculationSheetModel
@using Nop.Plugin.Misc.ExportAttributes.Models;
@using Nop.Web.Framework;
@using Nop.Core.Domain.Catalog;
@using (Html.BeginForm())
{
<table class="adminContent">
<tr>
<td colspan="2">
<b>Filter op Categorie:</b>
</td>
</tr>
<tr>
<td class="adminTitle">
@Html.NopLabelFor(model => model.searchCategory):
</td>
<td class="adminData">
@Html.DropDownListFor(x => x.searchCategory, new SelectList(ViewBag.CategoryList, "Name"))
</td>
</tr>
</table>
これは機能し、DropDownList には正しい値が入力されます。しかし、ViewBag が「ベスト プラクティス」であるとは思いません。モデルを使用してリストを選択することについて聞いたことがあります。ビューにモデルへの参照を既に追加しています。
@model ExportCalculationSheetModel
モデル クラスのリストに入力してビューで使用するにはどうすればよいですか?
私はすでに次の方法でモデルクラスを埋めようとしましたが、うまくいきませんでした:
public List<string> AllCategories
{
get
{
return AllCategories;
}
set
{
var _engine = new NopEngine();
var categoryService = _engine.Resolve<ICategoryService>();
var allCategory = categoryService.GetAllCategories();
List<string> ConvertedList = new List<string>();
for (int i = 0; i < allCategory.Count; i++)
{
ConvertedList.Add(allCategory[i].Name);
}
AllCategories = ConvertedList;
}
}
したがって、主な質問は次の 2 つです。
モデル ページのリストに入力するにはどうすればよいですか?
モデル ページのリストをビューのドロップダウン リストに接続するにはどうすればよいですか?
前もって感謝します!