2015-10-13 82 views
0

我正在使用Intranet,特別是每週監控工具,該工具可讓我逐周看到我的員工的活動。我正在致力於逐周過濾器。到目前爲止,我已經找到了如何創建一個包含List 2015年每星期按以下格式:DownDownList問題顯示類型

From dd/mm/yyyy to dd/mm/yyyy 

像這樣:

public ActionResult WeeklyMonitoring() 
{ 
    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState)); 

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list 
    ViewBag.Weeks = wl; 
    wl.Select(m => new SelectListItem { Text = m, Value = m });   
    return View(); 
} 

但這裏的問題,當我選擇一個星期我DropDownList(這裏我不得不將其轉換爲正確的類型):

<td><%: Html.DropDownList("Weeks", ((List<string>)ViewBag.Weeks).Select(m => new SelectListItem { Text = m, Value = m }), new { onchange = "this.form.submit();"}) %></td> 

它發送至:

[HttpPost] 
public ActionResult WeeklyMonitoring(FormCollection form) 
{ 
    string week = form["Weeks"];    

    ViewBag.Weeks = week; 
    return View(); 
} 

我得到了明顯的以下錯誤:

無法轉換類型 '字符串' 到 'System.Collections.Generic.List'

我怎麼可能避免這個錯誤,並保持表格發送後我的DropDownList選定的一週?

編輯:

到目前爲止,我已經發現了這個解決方案,可能不是一個過濾器實用但在這裏我去:

[HttpPost] 
public ActionResult WeeklyMonitoring(FormCollection form) 
{ 
    string week = form["Weeks"];   

    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState)); 

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); 
    wl.Remove(week); 
    wl.Insert(0, week); 
    wl.Select(m => new SelectListItem { Text = m, Value = m }); 
    ViewBag.Weeks = wl; 
    return View(); 
} 

我從列表中刪除的項目,並把它放在頂部,所以它由DDList顯示。

EDIT2:

添加FetchWeeks

public List<string> FetchWeeks(int year) 
    { 
     List<string> weeks = new List<string>(); 
     DateTime startDate = new DateTime(year, 1, 1); 
     startDate = startDate.AddDays(1 - (int)startDate.DayOfWeek); 
     DateTime endDate = startDate.AddDays(6); 
     while (startDate.Year < 1 + year) 
     { 
      weeks.Add(string.Format("From {0:dd/MM/yyyy} to {1:dd/MM/yyyy}", startDate, endDate)); 
      startDate = startDate.AddDays(7); 
      endDate = endDate.AddDays(7); 
     } 
     return weeks; 
    } 

EDIT3:由於kikyalex我們已經找到了解決辦法:

當ViewBag屬性具有相同的名稱創建一個問題DDList阻止它改變它的價值。

回答

1
string week = form["Weeks"];    

ViewBag.Weeks = week; 

從下拉列表中獲取當前星期並將其傳遞到視圖中。然後你把它作爲List<string>。發生錯誤是正常的。

您應該SelectListItem

public ActionResult WeeklyMonitoring() 
{ 
    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState)); 

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list 
    ViewBag.Weeks = wl.Select(m => new SelectListItem { Text = m, Value = m }).ToList(); 
    return View(); 
} 

[HttpPost] 
public ActionResult WeeklyMonitoring(FormCollection form) 
{ 
    string week = form["Weeks"];   

    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState)); 

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); 
    ViewBag.Weeks = wl.Select(m => new SelectListItem { Text = m, Value = m, Selected = m == week }).ToList(); 
    return View(); 
} 

在您查看鑄造改爲List<SelectListItem>而不是List<string>

<td><%: Html.DropDownList("Weeks", ((List<SelectListItem>)ViewBag.Weeks), new { onchange = "this.form.submit();"}) %></td> 
+0

我似乎仍然得到'無法將類型「使用System.Collections中選定的屬性。 Generic.List '到'System.Collections.Generic.List '後檢查並更改它,如你所說。 – Christopher

+1

你說得對。我忘了更改GET方法了。更新我的回答 –

+0

沒有更多的錯誤!但它仍然使我回到以前的日期。(不會更改爲選定的一個)。 ViewBag.Weeks現在不包含選定的一個嗎?編輯:我現在星期包含星期列表,但如何形式不會讓DDList框中選定的一週? – Christopher