【问题标题】:asp.net mvc Dropdownlist, showing dropdownlist, but no items in itasp.net mvc Dropdownlist,显示下拉列表,但里面没有项目
【发布时间】:2014-07-21 07:59:52
【问题描述】:

我有这个:

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

模型视图:

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>

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

Model.FAQCategorieItems:count = 0。但是,如果我在控制器中设置断点,我会看到数据库中的所有项目。

谢谢。

【问题讨论】:

  • 你正在返回 return View(model);.. 是不是就像 return View(categories);
  • 嗨,尼尔,但后来我收到了这个错误:
  • 您好 Neel,此错误:“/Medicijnverstrekking”应用程序中的服务器错误。传入字典的模型项的类型为“System.Collections.Generic.List`1[Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel]”,但此字典需要类型为“Multitask.Regenboog”的模型项.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel'。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
  • 你能在视图中显示你绑定模型的行吗?
  • 这里是:

    @Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:") @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems)

    但在控制器中我看到了这些项目,但在视图中它显示:count 0:Model.FAQCategorieItems

标签: c# asp.net-mvc asp.net-mvc-4 razor html.dropdownlistfor


【解决方案1】:

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

model= new FaqOverviewModel(categories);

之前

return View(model);

【讨论】:

  • 你可以尝试删除只读,然后将 IEnumerable 更改为 List。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
相关资源
最近更新 更多