【发布时间】:2014-04-22 13:37:03
【问题描述】:
我在如何在我的数据库中显示值的下拉列表时遇到问题。我的数据库表(类别)包含 2 列,即类别的 Id 和 Title。目标是提供一个下拉列表,用户可以在其中选择存储在表格中的标题。
型号:
public partial class Category
{
public int Id { get; set; }
public string Title { get; set; }
}
控制器:
public ActionResult Index()
{
private Context db = new Context();
ViewBag.Category = new SelectList(db.Categories, "Id", "Title");
return View();
}
查看:
@model MyApp.Models.Category
<p>
Categories: @Html.DropDownList("Id", "Select a Category")
</p>
我得到的错误是在视图中显示:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Id'.
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 drop-down-menu