有型无ViewData的项目'的IEnumerable< SelectListItem>'具有关键的“类别ID” [英] There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID'

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

问题描述

这是我的控制器动作

 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>

但是当我试图访问下拉值控制器我得到这个错误
有型无ViewData的项目'IEnumerable的'有'类别ID'的关键。

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'."

推荐答案

查看应该是这样的。

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

这篇关于有型无ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'具有关键的“类别ID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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