没有类型为'IEnumerable <selectlistitem>'的ViewData项具有“客户端”键

时间:2015-08-05 11:17:40

标签: c#

我有一个变量,我填充了数据库中的记录。然后,我将在视图上显示此列表作为下拉框。但是,一旦达到下拉,它就会失败。

控制器:

  public ActionResult Review() {
            var reviews = reviewRepo.GetAllReviews();

            var clients = clientRepo.Clients();    

            List<SelectListItem> items = new SelectList(clients, "ClientID", "ClientName").ToList();
            items.Insert(0, (new SelectListItem { Text = "Default", Value = "0" }));
            ViewBag.ListOfClients = items;

            IEnumerable<AdminReviewViewModel> model = reviews.Select(s => new AdminReviewViewModel() {
                ClientID = s.ClientID,
                ReviewID = s.ReviewID,
                ReviewName = s.ReviewName,
                ReviewPeriodID = s.ReviewPeriodID,
                ReviewPeriodName = s.ReviewPeriod
            });

            return View(model);
        }

查看:

@using (Html.BeginForm("Review", "UserAdmin", FormMethod.Post)) {
    @Html.DropDownList("Clients", ViewBag.ListOfClients as SelectList, new { @class = "GRDropDown", @id = "ReportDD"})
    <button type="submit" value="Submit" class="btn btn-default">Submit</button>
}

1 个答案:

答案 0 :(得分:1)

修正了它。错误消息中的线索非常多。它抱怨说没有客户。这是因为在我的下拉框中期待ClientID和ClientName。但相反,它接收价值和文本。所以我改变了我的下拉看起来像这样:

 @Html.DropDownList("Clients", new SelectList(ViewBag.ListOfClients, "Value", "Text"), new { @class = "GRDropDown", @id = "ReportDD" })