2011-09-29 30 views
2

當代碼跳轉到CreateProduct()時,填充的catList始終爲Count = 0,因此我認爲它不會傳遞。 考慮RouteValueDictionary不這樣做?任何其他方式?MVC RedirectToAction()以任何方式將對象傳遞給目標操作?

 public ActionResult GetCats(int CatID) 
    { 

     List<Category> catList = new List<Category>(); 

     if (CatID >= 0) 
     { 

      catList = context.Categories.Where(x => x.PCATID == CatID).ToList(); 
     } 


     return RedirectToAction("CreateProduct", "Admin", new { catList }); 
    } 





public ActionResult CreateProduct(List<Category> catList) {  } 

回答

4

你實際上是試圖用控制器來進行數據訪問。

將「GetCats」數據檢索移動到您的業務層(服務對象,知識庫,無論適合您)。

然後,CreateProduct將需要在那裏兩次(2個簽名)。其中一個沒有參數,您將在其中從業務層調用「GetCats」並將其發送到視圖。

其他實現將被標記爲HttpPostAttribute,並將在參數中包含創建cat的所有必要信息。

就是這樣。簡單而簡單。

1

RedirectToAction只是返回一個"302"代碼的URL重定向到瀏覽器。發生這種情況時,瀏覽器會使用該URL執行GET。

您的RouteValues需要很簡單。您無法使用路由值真正傳遞複雜對象或集合。

2

您可以在TempData中放置任何需要的物品,然後致電RedirectToAction

1

,如果你不關心瀏覽器的URL變化,你可以只

return CreateProduct(catList

+0

我同意返回視圖或部分視圖,而不是重定向到操作。渲染操作並在過度使用時重定向到操作可能會影響應用程序的性能。 –

相關問題