2013-05-09 89 views
6

我想發送一個字符串和模型(對象)到另一個動作。傳遞模型和參數與RedirectToAction

var hSM = new HotelSearchModel(); 
hSM.CityID = CityID; 
hSM.StartAt = StartAt; 
hSM.EndAt = EndAt; 
hSM.AdultCount = AdultCount; 
hSM.ChildCount = ChildCount; 

return RedirectToAction("Search", new { culture = culture, hotelSearchModel = hSM }); 

當我使用new關鍵字它發送null對象,雖然我設置對象hSm屬性。

這是我Search行動:

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel) 
{ 
    // ... 
} 

回答

13

你不能用一個RedirectAction發送數據。 這是因爲您正在執行301重定向,並返回給客戶端。

你需要什麼是它保存在TempData的:

var hSM = new HotelSearchModel(); 
hSM.CityID = CityID; 
hSM.StartAt = StartAt; 
hSM.EndAt = EndAt; 
hSM.AdultCount = AdultCount; 
hSM.ChildCount=ChildCount; 
TempData["myObj"] = new { culture = culture,hotelSearchModel = hSM }; 

return RedirectToAction("Search"); 

之後,你可以從TempData的再次檢索:

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel) 
{ 
    var obj = TempData["myObj"]; 
    hotelSearchModel = obj.hotelSearchModel; 
    culture = obj.culture; 
}