0

我想重定向到同一控制器中的另一个动作并传递一个参数值。我有这个代码:

public ActionResult Index()
{

}

public ActionResult SomeAction()
{
  if (isFailed)
  {
    // Redirect to Index Action with isFailed parameter so that some message gets displayed on Index page.
  }
 }

我阅读了有关使用的信息TempData,但我的会话是只读的(出于某种目的),因此如果我将数据保存在TempDatain 中SomeAction,它无济于事,因为它实际上TempData是空的Index

我尝试的另一件事是RedirectToAction("Index","Test", new { param = isFailed})return声明中使用SomeAction. 这可行,我可以使用实际访问param,但问题是 url 现在变成了我想要的位置。IndexRequest.QueryString['param']/AreaName?param=true/AreaName/Test/

这是我的路线图:

        context.MapRoute(
            "default",
            "AreaName/{controller}/{action}/{param}",
            new { controller="Test", action = "Index", param=UrlParameter.Optional },
        );

我正在使用 MyJS.js 中的“post”将表单提交到SomeAction.

是否有任何替代/解决方法可以做到这一点?简而言之,我想要这三件事:

  1. 重定向到Index行动。
  2. param将from的值传递SomeActionIndex
  3. 保留网址:http://localhost/AreaName/Test/
4

2 回答 2

1

Try this

public ActionResult Index(Datatype param)
{

}
public ActionResult SomeAction()
{
if (isFailed) 
{
     return RedirectToAction("Index" , "Home",new{param= value });
}    
return View();
}
于 2013-06-18T17:56:16.020 回答
0
  • 重定向到Index行动。

利用return RedirectToAction("Index","Home");

  • 将 param 的值从SomeActionto传递给Index

您可以使用TempData对象。

TempData 属性值存储在会话状态中。在设置 TempDataDictionary 值之后调用的任何操作方法都可以从对象中获取值,然后处理或显示它们。TempData 的值会一直存在,直到它被读取或会话超时。

你可以这样做:

public ActionResult Index()
{
    //you can access TempData here.
}

public ActionResult SomeAction()
{
  if (isFailed)
  {
      TempData["Failure"] = "Oops, Error";  //store to TempData
      return RedirectToAction("Index" , "Home");
  }
  return View();
 }

这篇 MSDN 文章解释了这一切。

  • 保留网址:http://localhost/AreaName/Test/

您可以通过在您的RouteConfig.cs

context.MapRoute(
            "default",
            "AreaName/Test/",
            new { area = "AreaName" controller="Test", action = "Index"},
        );
于 2013-06-18T17:34:25.193 回答