C# asp.net mvc Dropdownlist,显示Dropdownlist,但其中没有项目
我有这个:C# asp.net mvc Dropdownlist,显示Dropdownlist,但其中没有项目,c#,asp.net-mvc,asp.net-mvc-4,razor,html.dropdownlistfor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,Html.dropdownlistfor,我有这个: FaqCategorieService faqCategorieService; public FAQController(FaqCategorieService faqCategorieService) { this.faqCategorieService = faqCategorieService; } // GET: FAQ [HttpGet] public ActionResult Index(Faq
FaqCategorieService faqCategorieService;
public FAQController(FaqCategorieService faqCategorieService)
{
this.faqCategorieService = faqCategorieService;
}
// GET: FAQ
[HttpGet]
public ActionResult Index(FaqOverviewModel model)
{
var categories = faqCategorieService.GetAll().OrderBy(x => x.Naam)
.Select(a => new FaqOverviewModel()
{
Id = a.Id,
Naam = a.Naam
}).ToList();
foreach (var categorie in categories)
{
categorie.Naam = categorie.Naam + " (" + categorie.Id.ToString() + ")";
}
categories.Insert(0, new FaqOverviewModel() { Id = -1, Naam = "Maak een keuze..." });
//var cats = faqCategorieService.GetAll().ToList();
return View(model);
// return RedirectToAction("Index");
}
模型视图:
视图:
支持
我看到了dropdownlist,但现在其中有一些项目
Model.FAQCategorieItems:计数=0。但是如果我在控制器中设置断点,我会看到数据库中的所有项目
谢谢。您正在做的是将您的项目添加到列表中。但是列表中的项必须映射到模型的属性。 添加行
model= new FaqOverviewModel(categories);
以前
return View(model);
您正在返回return Viewmodel;。。这不是很像返回视图类别;Hi-Neel,但是我得到了这个错误:Hi-Neel,这个错误:“/MedicijnVerstreking”应用程序中的服务器错误。传递到字典中的模型项的类型为“System.Collections.Generic.List`1[Multitask.Regenboog.MedicijnVersTreking.WebApplication.ViewModels.FAQ.FaqOverviewModel]”,但此字典需要类型为“Multitask.Regenboog.MedicijnVersTreking.WebApplication.ViewModels.FAQ.FaqOverviewModel”的模型项。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的详细信息以及错误源于代码的位置。能否在视图中显示绑定模型的行?即:@Html.LabelFormodel=>model.SelectedCategoriedFaqId,Categorie:@Html.DropDownListForx=>x.SelectedCategoriedFaqId,Model.faqcategoriteItems但在控制器中我看到了这些项目,但在视图中显示:计数0:Model.faqcategoriteItems尝试删除只读,然后将IEnumerable更改为列表。
model= new FaqOverviewModel(categories);
return View(model);