没有类型的 ViewData 项目...问题
There is no ViewData item of type... issue
我正在内部网上工作,准确地说是在一个 activity 报告系统上,该系统列出了一名员工每周 activity 的工作。所以我可以 select 有问题的员工 DropDownList
但我遇到了 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
填写:
<td> <%:Html.DropDownList("Employee", (List<SelectListItem>)ViewBag.EmployeeList, "", new { onchange = "this.form.submit();" })%></td>
但是当我 select 一个 Employee
它给了我以下错误:
System.InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the key 'Employee'.
我觉得很奇怪,因为列表 填满了名字。
错误发生是因为 ViewBag.EmployeeList
的值为 null
(在这种情况下 DropDownList()
的回退是使用第一个参数作为 IEnumerable<SelectListItem>
属性).
由于您已在 GET 方法中正确填充它,因此发生错误是因为您 return 在 POST 方法中的视图并且没有重新分配 ViewBag.EmployeeList
[ 的值=15=]
我正在内部网上工作,准确地说是在一个 activity 报告系统上,该系统列出了一名员工每周 activity 的工作。所以我可以 select 有问题的员工 DropDownList
但我遇到了 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
填写:
<td> <%:Html.DropDownList("Employee", (List<SelectListItem>)ViewBag.EmployeeList, "", new { onchange = "this.form.submit();" })%></td>
但是当我 select 一个 Employee
它给了我以下错误:
System.InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the key 'Employee'.
我觉得很奇怪,因为列表 填满了名字。
错误发生是因为 ViewBag.EmployeeList
的值为 null
(在这种情况下 DropDownList()
的回退是使用第一个参数作为 IEnumerable<SelectListItem>
属性).
由于您已在 GET 方法中正确填充它,因此发生错误是因为您 return 在 POST 方法中的视图并且没有重新分配 ViewBag.EmployeeList
[ 的值=15=]