没有“IEnumerable<SelectListItem>"类型的 ViewData 项具有键“CategoryID"的 [英] There is no ViewData item of type &#39;IEnumerable&lt;SelectListItem&gt;&#39; that has the key &#39;CategoryID&#39;

查看:17
本文介绍了没有“IEnumerable<SelectListItem>"类型的 ViewData 项具有键“CategoryID"的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器动作

 public ActionResult AddNewPost(string subName)
        {
          ViewBag.CategoryID = new SelectList(from c in db.Categories where c.Status == true select c, "CategoryID", "CategoryName");
          return View();
        }

在视图中,我已将下拉列表填充为

in view i have populated dropdown as

<td>
    @Html.DropDownList("CategoryID", "-- Select --")
</td>

但是当我尝试访问控制器中的下拉值时,我收到此错误没有‘IEnumerable’类型的 ViewData 项具有键‘CategoryID’."

but when i try to access the drop down value in controller i get this error "There is no ViewData item of type 'IEnumerable' that has the key 'CategoryID'."

推荐答案

view应该是这样的

@Html.DropDownListFor(item =>Model.CategoryID,ViewBag.CategoryId as SelectList,"-- Select --")

这篇关于没有“IEnumerable<SelectListItem>"类型的 ViewData 项具有键“CategoryID"的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆