我正在使用MVC 2和实体框架4.在我的创建应用程序页面上,我有一个下拉列表,其中填充了我的AccountType枚举中的值。这是我做的:ASP.NET MVC 2下拉问题
public ActionResult Create()
{
// Get the account types from the account types enum
var accountTypes = from AccountType at
in Enum.GetValues(typeof(AccountType))
select new
{
AccountTypeID = (int)Enum.Parse(typeof(AccountType), at.ToString()),
AccountTypeName = GetEnumFriendlyName(at)
};
ViewData["AccountTypes"] = new SelectList(accountTypes, "AccountTypeID", "AccountTypeName");
return View();
}
这是我的代码看起来像这个下拉列表中的数据:
<%= Html.DropDownList("AccountTypeID", (SelectList)ViewData["AccountTypes"], "-- Select --") %>
页面加载,我开始输入一些数据后。我从下拉列表中选择一个值。当输入所有必需的输入时,我点击提交。下面是只是一块代码:
[HttpPost]
public ActionResult Create(Application application)
{
if (ModelState.IsValid)
{
application.ApplicationState = (int)State.Applying;
}
return View();
}
然后我碰到下面的错误,不知道这意味着什么,但我没有谷歌它,尝试了样品,但我仍然得到消息。以下是错误消息:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'AccountTypeID'.
我甚至改变了下拉列表的视图中的:
<%= Html.DropDownList("AccountTypeID", (IEnumerable<SelectListItem>)ViewData["AccountTypes"], "-- Select --") %>
我不知道我做错了吗?我将不胜感激:)
谢谢。
我想应用程序类包含AccountTypeID属性?错误发生在POST上,或者是什么?问题有点模糊.. – mare 2010-09-13 14:03:00