2013-10-04 36 views
2

我的每個控制器方法都需要重定向回到索引頁面,並將它們發回的模型對象發送回控制器。但是,在一個實例中,我需要與模型對象一起發送錯誤消息。下面是索引方法的簽名:無法在RedirectToAction上傳遞多個參數

public ViewResult Index(ZipCodeIndex search, string unspecifiedAction = "") 

由於我只需要一個方法的錯誤消息,我使這個參數可選。這裏是我想從單獨行動重定向到索引:

 //the parameter 'updateZip' is a model object of type ZipCodeIndex 
     return RedirectToAction("Index", new { search = updateZip, unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."}); 

這一切都捲起做的是將用戶發送回並顯示錯誤消息的原始頁面「對象引用未設置爲實例一個東西。」

EDIT

控制器後擊中RedirectToAction它簡單地退出所述控制器,而不重定向到索引方法,和錯誤「不設置到對象的實例對象refrerence」出現在視圖。

+0

沒有什麼是錯的代碼,您是否收到任何錯誤? – Satpal

+0

什麼是'ZipCodeIndex'? – Satpal

+0

我認爲問題是「search = updateZip」。請刪除此參數並重試。告訴我們結果。 –

回答

3

您無法傳遞RedirectToAction中的類對象,因此請刪除search = updateZip參數。

如果你需要它。您可以在TempData把它作爲一種替代

修改你這種行爲

public ViewResult Index(string unspecifiedAction = ""){ 
     var search = (ZipCodeIndex)TempData["ZipCodeIndexData"]; 
     //rest of code 
} 

重定向

TempData["ZipCodeIndexData"] = updateZip; 
return RedirectToAction("Index", new { unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."}); 
+0

仍然收到相同的錯誤。 – NealR

+0

你有什麼錯誤?嘗試通過控制器與路由 – Satpal

+0

控制器不重定向到索引方法,並在視圖上我收到一條錯誤消息,說'對象引用未設置爲對象的實例' – NealR