【发布时间】:2010-01-25 01:20:30
【问题描述】:
我是 MVC 和 ASP.NET 的新手,想学点东西,所以我尝试制作一些琐碎的应用程序来了解其中的来龙去脉。
好吧,我正在尝试让一个下拉框显示书籍列表,它会显示书名但发布 book_id [主键]
我得到的错误是:没有 ViewData 项,键为“IEnumerable”类型的键“book_id”。
这是我的看法:
<p>
<label for="book_id">Book:</label>
<%= Html.DropDownList("book_id" , (IEnumerable<SelectListItem>)ViewData["Books"]) %>
<%= Html.ValidationMessage("book_id", "*") %>
</p>
这是我的控制器中的内容
// 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();
}
}
我的 Books 表看起来像这样
book_id
title
publisher
language
isbn
我假设这是一个微不足道的新手错误;但我没有太多运气弄清楚。任何帮助都会很棒
【问题讨论】:
-
请在我的回复中查看缺少的行的附加代码(为简单起见,在 catch 语句中)
标签: c# asp.net-mvc