2009-04-29 37 views
1

我目前正在使用的ASP.net頁面有一個下拉列表,旨在有一個過濾器列表。當用戶選擇過濾器時,我想顯示一個具有適合過濾器屬性的用戶控件。動態選擇一個Asp.Net MVC用戶控件

這裏是有問題的控制器動作:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Index(FormCollection collection) 
{ 
    var filterType = Request.Form["FilterSelect"]; 
    ViewData["FilterChosen"] = filterType; 
    PopulateSelectionFiltersData();//This method fills up the drop down list 
    //Here is where I would like to switch based on the filterType variable 
    return View(); 
} 

過濾器類型變量具有正確的價值,但我不確定要怎麼做下一部分。

此外,作爲一個必然的問題,在調用之間保持所選下拉值的最佳方式是什麼?

非常感謝,

KevDog

回答

3

存儲正確的控制ViewData的顯示。至於持久菜單, 你的選擇是緩存(許多會話使用),會話(僅用於此會話)或TempData(僅用於此會話中的下一個方法)。或者,您可以將它緩存到DataLayer中。通常情況下,我只是重新提取數據,直到它成爲性能問題 - 通常不會。

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Index(FormCollection collection) 
{ 
    var filterType = Request.Form["FilterSelect"]; 
    ViewData["FilterChosen"] = filterType; 
    PopulateSelectionFiltersData();//This method fills up the drop down list 

    string userControl = "DefaultControl"; 
    switch (filterType) 
    { 
     case "TypeA": 
     userControl = "TypeAControl"; 
     break; 
     ... 
    } 

    ViewData["SelectedControl"] = userControl; 
    return View(); 
} 


<% Html.RenderPartial(ViewData["SelectedControl"], Model, ViewData); %> 
+0

需要改變的RenderPartial消除等號,並在末尾添加一個分號。否則,這是完美的。 – KevDog 2009-04-29 14:23:55