MVC-没有键类型为"IEnumerable< SelectListItem>"的Viewdata项 [英] MVC - No Viewdata item with key of type 'IEnumerable<SelectListItem>'
问题描述
我是MVC和ASP.NET的新手,并且希望学习一些东西,所以我试图制作一些琐碎的应用程序来了解这些细节.
I'm new to MVC and ASP.NET and am looking to learn a bit, so I'm trying to make some trivial applications to learn the ins and outs.
好吧,我正在尝试使一个下拉框显示一本书的列表,在其中可以显示书的标题,但要发布book_id [主键]
Well, I'm trying to make a dropdown box show a list of books where it would show the title of the book but post the book_id [primary key]
我得到的错误是::没有键类型为'IEnumerable'的键'book_id'的ViewData项目.
The error I get is:: There is no ViewData item with the key 'book_id' of type 'IEnumerable'.
这是我的观点:
<p>
<label for="book_id">Book:</label>
<%= Html.DropDownList("book_id" , (IEnumerable<SelectListItem>)ViewData["Books"]) %>
<%= Html.ValidationMessage("book_id", "*") %>
</p>
这是我的控制器中的内容
Here is what's in my controller
// GET: /Home/Create
//This is the form creation.
[Authorize]
public ActionResult Create()
{
this.ViewData["Books"] =
new SelectList(_entities.BookSet.ToList(), "book_id", "Title");
return View();
}
//
// POST: /Home/Create
//This sends it to the DB
[AcceptVerbs(HttpVerbs.Post) , Authorize]
public ActionResult Create([Bind(Exclude="problem_id")] Problem inProblem)
{
try
{
// TODO: Add insert logic here
Models.User user = getUser(User.Identity.Name);
if (user != null)
{
inProblem.user_id = user.user_id;
}
_entities.AddToProblemSet(inProblem);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
我的图书"表如下所示:
And my Books table looks like this
book_id
title
publisher
language
isbn
我认为这是一个新手的小错误;但我算不上什么运气.任何帮助都很好
I'm assuming this is a trivial newbie mistake; but I'm not having much luck figuring it out. Any help would be great
推荐答案
更简单的解决方案是将ViewData元素的名称与下拉列表相同:
Easier solution is to just name the ViewData element the same as the drop down list:
this.ViewData["book_id"] = new SelectList(_entities.BookSet.ToList(), "book_id", "Title");
这将自动绑定:
<%= Html.DropDownList("book_id") %>
如果您想再次显示视图(如在捕获中一样),还需要在Create的Post版本中填充ViewData ["book_id"].
You will also need to populate the ViewData["book_id"] in the Post version of Create if you want to display the view again (as you do in the catch).
[AcceptVerbs(HttpVerbs.Post) , Authorize]
public ActionResult Create([Bind(Exclude="problem_id")] Problem inProblem)
{
try
{
// TODO: Add insert logic here
Models.User user = getUser(User.Identity.Name);
if (user != null)
{
inProblem.user_id = user.user_id;
}
_entities.AddToProblemSet(inProblem);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
this.ViewData["Books"] = new SelectList(_entities.BookSet.ToList(), "book_id", "Title");
// ViewData["book_id"] with example above.
return View();
}
}
注意:上面的this.ViewData ["Books"]应该确实在捕获范围内完成-在那里只是为了演示丢失的内容.
Note: The this.ViewData["Books"] above should really be done within the catch - its just there for demonstration on whats missing.
这篇关于MVC-没有键类型为"IEnumerable< SelectListItem>"的Viewdata项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!