2015-10-13 49 views
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>&nbsp;&nbsp;&nbsp;<%:Html.DropDownList("Employee", (List<SelectListItem>)ViewBag.EmployeeList, "", new { onchange = "this.form.submit();" })%></td> 

但是,當我選擇Employee它給了我下面的錯誤:

System.InvalidOperationException:沒有類型爲'IEnumerable'的ViewData項具有'Employee'鍵。

我覺得很奇怪,因爲名單確實是填上了名字。

+1

價值,因爲'ViewBag.EmployeeList'是'null'(最有可能是因爲您提交併在POST方法中返回視圖並且沒有重新填充「ViewBag.EmployeeList」) –

+0

謝謝!請張貼它作爲答案,以便我可以驗證它。 – Christopher

回答

1

該錯誤發生的原因的ViewBag.EmployeeList值爲null(在這種情況下的DropDownList()回退是使用第一參數作爲IEnumerable<SelectListItem>屬性)。

既然你已經在正確的GET方法填充它,錯誤的現象發生,因爲你在POST方法返回的觀點,並沒有重新分配的ViewBag.EmployeeList

相關問題