简体   繁体   English

重定向到同一控制器中的另一个动作并传递一些数据asp.net mvc3

[英]Redirecting to another action in same controller and passing some data asp.net mvc3

I want to redirect to another action in same controller and pass one parameter value. 我想重定向到同一控制器中的另一个动作并传递一个参数值。 I have this code: 我有以下代码:

public ActionResult Index()
{

}

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

I read about using TempData but my session is read only (for some purpose) thus I if I save data in TempData in SomeAction , it does not help since TempData is empty in Index action. 我读过有关使用TempData但是我的会话是只读的(出于某种目的),因此,如果我将数据保存在TempData中的SomeActionTempData SomeAction帮助,因为Index动作中的TempData为空。

Another thing I tried is using RedirectToAction("Index","Test", new { param = isFailed}) in return statement of SomeAction . 我尝试的另一件事是在SomeAction return语句中使用RedirectToAction("Index","Test", new { param = isFailed}) This works and I can access param in Index action using Request.QueryString['param'] but the issue with this is the url now becomes /AreaName?param=true where I want it to be /AreaName/Test/ . 这工作,我可以访问paramIndex使用动作Request.QueryString['param']但这个问题是url现在变为/AreaName?param=true ,我希望它是/AreaName/Test/

This is my routemap: 这是我的路线图:

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

I am submitting a form using "post" from MyJS.js to SomeAction . 我正在使用MyJS.js中的“ post”向SomeAction提交SomeAction

Is there any alternative/workaround method for doing this? 是否有其他替代方法/解决方法? In nutshell, I want these three things: 简而言之,我想要这三件事:

  1. Redirect to Index action. 重定向到Index操作。
  2. Pass the value of param from SomeAction to Index . param的值从SomeActionIndex
  3. Keep the URL: http://localhost/AreaName/Test/ 保留网址: http://localhost/AreaName/Test/

Try this 尝试这个

public ActionResult Index(Datatype param)
{

}
public ActionResult SomeAction()
{
if (isFailed) 
{
     return RedirectToAction("Index" , "Home",new{param= value });
}    
return View();
}
  • Redirect to Index action. 重定向到Index操作。

Use return RedirectToAction("Index","Home"); 使用return RedirectToAction("Index","Home");

  • Pass the value of param from SomeAction to Index . 将param的值从SomeActionIndex

You can use the TempData object. 您可以使用TempData对象。

The TempData property value is stored in session state. TempData属性值存储在会话状态中。 Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. 设置TempDataDictionary值后调用的任何操作方法都可以从对象获取值,然后对其进行处理或显示。 The value of TempData persists until it is read or until the session times out. TempData的值将一直保留,直到被读取或会话超时为止。

You can do it as below: 您可以按照以下步骤进行操作:

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();
 }

This MSDN article explains it all. 这篇MSDN文章解释了这一切。

  • Keep the URL: http://localhost/AreaName/Test/ 保留网址: http://localhost/AreaName/Test/

You can do so by adding a route in your RouteConfig.cs 您可以通过在RouteConfig.cs添加路由来RouteConfig.cs

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从同一控制器ASP.NET MVC3重定向以查看或调用void方法 - Redirecting to view or call void method from same controller ASP.NET MVC3 在ASP.NET MVC中将数据从一个控制器传递到另一个 - Passing data from one controller to another in asp.net mvc ASP.Net MVC3 Dropdownlist和传递数据 - ASP.Net MVC3 Dropdownlist and passing data 将数据从一个控制器动作传递到ASP.Net MVC 5中的另一个控制器动作 - pass data from one controller action to another controller action in ASP.Net MVC 5 当客户端超时取消控制器操作时,ASP.NET MVC3 应用程序将如何调用某些代码? - How would an ASP.NET MVC3 application call some code when controller action is cancelled on client timeout? 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 重定向到动作ASP.NET MVC3 - Redirection to action asp.net mvc3 ASP.NET MVC从同一控制器发布到控制器操作 - ASP.NET MVC post to controller action from same controller 将接口传递给ASP.NET MVC Controller Action方法 - Passing an interface to an ASP.NET MVC Controller Action method ASP.Net MVC 4将onSessionStart重定向到Action - ASP.Net MVC 4 Redirecting onSessionStart to an Action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM