asp.net mvc下拉列表,显示下拉列表,但其中没有项目

本文关键字:下拉列表 项目 显示 net mvc asp | 更新日期: 2023-09-27 18:13:53

我有这个:

    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"); 
    }

ModelView:

public class FaqOverviewModel
{
    public int Id { get; set; }
    public string EmailBericht { get; set; }
    public string Naam { get; set; }
    public FaqOverviewModel()
    {
        Categorie = new List<FaqCategorie>();
    }
    public FaqOverviewModel(IEnumerable<FaqCategorie> categories)
    {
        this.Categorie = categories;
    }   
    #region FAQCategorie
    private readonly IEnumerable<FaqCategorie> Categorie;
    public int? SelectedCategoriedFaqId { get; set; }
    public IEnumerable<SelectListItem> FAQCategorieItems
    {
        get
        {
            return new SelectList(Categorie, "Id", "Naam");
        }
    }
    #endregion
    #region subcategorie
    #endregion
}

视图:

的支持
<p>
    <span class="fixedLabelWidth">@Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:")</span>
    @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems)
</p>   
<p>
    <span class="fixedLabelWidth">@Html.LabelFor(model => model.EmailBericht, "Bericht:")</span>
    @Html.TextAreaFor(x => x.EmailBericht)
</p>

我看到下拉列表,但是现在里面有项目了。

模型。FAQCategorieItems: count = 0。但是,如果我在控制器中放入一个断点,我将看到数据库中的所有项。

谢谢。

asp.net mvc下拉列表,显示下拉列表,但其中没有项目

您正在做的是将您的项目添加到列表中。但是列表中的项必须映射到模型的属性。添加行

model= new FaqOverviewModel(categories);

之前
return View(model);