My controller looks as follow:
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");
So basically I'm filling the ViewBag with a List of strings.
My view looks as follow:
@{
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>
This works, the DropDownList gets filled with the correct values. But I don't think that the ViewBag is "Best-Practice", I heard something about using a Model to select the List from, I already added a reference to the model in the view:
@model ExportCalculationSheetModel
How can I fill the list at the model class and use it in my view?
I already tried to fill the model class the following way, but that didn't work out:
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;
}
}
So the two primary questions are:
How do I fill the list at the model page?
How do I connect the list at my model page to the dropdownlist at my view?
Thanks in advance!