1
我正在使用Intranet,正是在一個活動報告系統上,該系統列出了員工每週的活動。因此,我可以用DropDownList
來選擇有問題的員工,但我在選擇時遇到問題。沒有ViewData類型的項目...問題
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>
但是,當我選擇Employee
它給了我下面的錯誤:
System.InvalidOperationException:沒有類型爲'IEnumerable'的ViewData項具有'Employee'鍵。
我覺得很奇怪,因爲名單確實是填上了名字。
價值,因爲'ViewBag.EmployeeList'是'null'(最有可能是因爲您提交併在POST方法中返回視圖並且沒有重新填充「ViewBag.EmployeeList」) –
謝謝!請張貼它作爲答案,以便我可以驗證它。 – Christopher