有型......问题没有ViewData的项目 [英] There is no ViewData item of type... issue
问题描述
我正在Intranet上,precisely活动报告制度,列出了员工每周的活动上。所以,我能够在问题与的DropDownList
选择员工,但我有与选择的问题。
I'm working on an intranet, precisely on an activity report system that lists the activity per week of an Employee. So I'm able to select the Employee in question with a DropDownList
but I'm having issues with the selection.
public ActionResult Client() // ReportIndex
{
[...]
string strEmployee = "-1";
string strUser = User.Identity.Name;
if (null != (string)Session["ReportEmployee"])
strEmployee = (string)Session["ReportEmployee"];
[...]
ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
//nea60
IEnumerable<SelectListItem> pl = pr.ListProject(strUser, strYear, strMonth);
IEnumerable<SelectListItem> pl2 = pr.ListEmployee();
foreach (SelectListItem item in pl)
if (item.Value == strProject)
item.Selected = true;
ViewBag.ProjectList = pl;
foreach (SelectListItem item in pl2)
if (item.Value == strEmployee)
item.Selected = true;
ViewBag.EmployeeList = pl2;
//nea11
Session["ReportYear"] = strYear;
Session["ReportMonth"] = strMonth;
//nea58
Session["ReportProject"] = strProject;
//nea58
return View();
}
正如你所看到的, PL2
包含雇员的名单与的帮助pr.ListEmployee()
,以下的DropDownList
填补了:
As you can see, pl2
contains the list of Employees with the help of pr.ListEmployee()
, the following DropDownList
fills up :
<td> <%:Html.DropDownList("Employee", (List<SelectListItem>)ViewBag.EmployeeList, "", new { onchange = "this.form.submit();" })%></td>
但是,当我选择一个员工
它给了我以下错误:
System.InvalidOperationException:有类型的无ViewData的项目'的IEnumerable具有关键的雇员
System.InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the key 'Employee'.
我觉得很奇怪,因为列表中的确实的以名字填满。
I find it odd since the list does fill up with the names.
推荐答案
错误发生,因为 ViewBag.EmployeeList
值为空
(在这种情况下,的DropDownList的回退()
是使用的第一个参数为的IEnumerable&LT; SelectListItem&GT;
属性)。
The error occurs because the value of ViewBag.EmployeeList
is null
(in which case the fallback of DropDownList()
is to use the first parameter as the IEnumerable<SelectListItem>
property).
既然你已经在正确的GET方法填充它,错误是发生,因为你返回POST方法的观点,并没有重新分配 ViewBag.EmployeeList
的价值
Since you have correctly populated it in the GET method, the error is occuring because you return the view in the POST method and have not reassigned the value of ViewBag.EmployeeList
这篇关于有型......问题没有ViewData的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!