2014-08-27 164 views
5

我這樣做了。如何使用RedirectToAction將對象作爲隱藏參數傳遞?

public ActionResult GetInfo(SomeModel entity) 
{ 
    ---- 
    return RedirectToAction("NewAction", "NewController", new System.Web.Routing.RouteValueDictionary(entity)); 
} 

作用,這被稱爲

public ActionResult NewAction(SomeModel smodel) 
{ 
    ------- 
    ------- 
} 

這是工作正常,但我可以看到瀏覽器地址欄上張貼的所有PARAM值,我怎麼能隱藏在瀏覽器的查詢字符串這些PARAM值。

http://localhost:51545/NewController/NewAction?SurveyID=13&CatID=1&PrimaryLang=1&SurveryName=Test%20Survery&EnableMultiLang=False&IsActive=False 

任何幫助將不勝感激。

回答

8

在你的情況,而不是使用RouteValueDictionary並通過從查詢字符串模式嘗試TempData(因爲當我們使用RedirectToAction它將使一個新的HTTP請求和對象路由在URL中顯示所以它不是一個好方法,以顯示敏感數據URL)。如圖所示

使用TempData: -

public ActionResult GetInfo(SomeModel entity) 
{ 
    ---- 
    TempData["entity"] = entity; //put it inside TempData here 
    return RedirectToAction("NewAction", "NewController"); 
} 

public ActionResult NewAction() 
{ 
    SomeModel smodel = new SomeModel(); 
    if(TempData["entity"] != null){ 
    smodel = (SomeModel)TempData["entity"]; //retrieve TempData values here 
    } 
    ------- 
    ------- 
} 

這裏使用TempData的好處是,它會保留其值一個重定向,而且模型將被私自運到另一個控制器動作,一旦你讀取數據TempData其數據將被自動設置,如果你想看完後保留TempData值,則使用TempData.keep("entity")


OR

如果你的意見是在同一個控制器那麼這對於您的問題,一個簡單的解決方案:

public ActionResult GetInfo(SomeModel entity) 
{ 
    ---- 
    return NewAction(entity); 
} 

public ActionResult NewAction(SomeModel smodel) 
{ 
    ------- 
    ------- 
    return View("NewAction",smodel) 
} 

正如@ Chips_100正確註釋,以便即時通訊,包括在這裏: - 該第一個解決方案會做一個真正的重定向(302),它將更新用戶瀏覽器中的URL。第二種解決方案將在原始URL保留在地址欄中的同時提供期望的結果。

+0

這看起來過於複雜。沒有辦法做服務器端重定向嗎? – 2014-08-27 08:27:25

+0

@ PhilipPittle..what複雜..只是你已經把模型內部的TempData,然後強制轉換成TempData的期望action..what模型,它是複雜的? – 2014-08-27 08:32:50

+0

拋出:由於它的工作原理 @菲利普Pittle:一些更有效的建議,如果有可以理解的。 – 2014-08-27 09:04:42

相關問題