我已经这样做了。
public ActionResult GetInfo(SomeModel entity)
{
----
return RedirectToAction("NewAction", "NewController", new System.Web.Routing.RouteValueDictionary(entity));
}
被调用的 Action
public ActionResult NewAction(SomeModel smodel)
{
-------
-------
}
这工作正常,但我可以在浏览器地址栏上看到所有发布的参数值,如何在浏览器中隐藏这些查询字符串参数值。
http://localhost:51545/NewController/NewAction?SurveyID=13&CatID=1&PrimaryLang=1&SurveryName=Test%20Survery&EnableMultiLang=False&IsActive=False
任何帮助将不胜感激。
最佳答案
在你的情况下,而不是使用 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
的好处这里是它会保留它对一个重定向的值,而且模型将私下传输到另一个 Controller 操作,一旦你从 TempData
读取数据它的数据将被自动处理,如果你想保留 TempData
读取后的值然后使用 TempData.keep("entity")
.或者
如果您的 View 在同一个 Controller 中,那么这是您问题的简单解决方案:
public ActionResult GetInfo(SomeModel entity)
{
----
return NewAction(entity);
}
public ActionResult NewAction(SomeModel smodel)
{
-------
-------
return View("NewAction",smodel)
}
正如@Chips_100 正确评论的那样,所以我将它包含在这里:- 第一个解决方案将执行真正的重定向(302),它将更新用户浏览器中的 URL。第二种解决方案将提供所需的结果,同时将原始 URL 保留在地址栏中。
关于asp.net-mvc - 如何使用 RedirectToAction 将对象作为隐藏参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25521541/