重定向到其他控制器动作 [英] Redirect to action in other controller

查看:123
本文介绍了重定向到其他控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个行动中的一个控制器重定向到一第二控制器的操作。通常我会用RedirectToAction(actionName,controllerName对象);
我想重定向的方法有两个重载:

I want to redirect from an action in one controller to an action in a second controller. Normally I would use RedirectToAction("actionName", "controllerName", objects); The method I want to redirect to has two overloads:


  • 一个用于HttpVerbs.Get是用于直接链接

  • 一个用于HttpVerbs.Post接受引用类型的打通modelbinding充满

当我做我的重定向与RedirectToAction方法,我重定向到系统默认的GET方法,它偏离轨道不匹配我的参数。

我怎样才能确保它重定向到正确动作的过载?

When I do my redirect with the RedirectToAction method, I get redirected to the GET method by default which off course doesn't match my parameters.
How can I make sure it redirects to the correct action's overload?

- 编辑 -

根据要求,一些更具体的细节:

我想重定向基于参数来填充的ViewData然后在操作调用正确的观点。

--EDIT--
On request some more specific details:
The action I want to redirect to fills the viewData based on the parameters and then calls the correct view.

public ActionResult OverView(SearchBag searchBag, IngredientBag ingredientBag) {

它有一个第二个版本获取,因此它可以通过太工作:

It has a second version for Gets so it can work by GET too:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult OverView(int colorId, string paintCode, string name, int formulaId) {
    return OverView(new SearchBag() 
        { ColorId = colorId, PaintCode = paintCode, ColorName = name, FormulaId = formulaId }
            , formulaViewData.IngredientBag);
}

的模式,现在我打电话是在不同的控制器。它确实有些pre-计算,获取所需要的信息,然后做同样的事情为previous行动。我可以从第一个动作复制code,但我宁愿只是调用该动作。

The one I'm calling now is in a different controller. It does some pre-calculations, fetches the information needed and then does the exact same thing as the previous actions. I could replicate the code from the first action, but I would rather just call that action.

[AcceptVerbs(HttpVerbs.Post)]
public RedirectToRouteResult ReCalculate(SearchBag searchBag, IngredientBag ingredientBag) {

我可以创建一个控制器的一个临时的本地实例,但我注意到它没有正确的HttpContext和不打初始化方法。

I could create a temporary local instance of that next controller, but I noticed it doesn't have the correct HTTPContext and doesn't hit Initialization methods.

推荐答案

您不能使用RedirectToAction(或其他东西)导致浏览器以HTTP POST重定向。您可以使用JavaScript来破解它,但它是丑陋的。

You can not use RedirectToAction (or anything else) to cause the browser to redirect with a HTTP POST. You may be able to hack it with some JavaScript but it would be ugly.

如果您可以提供有关您想将用户重定向到我们能为您提供更好的答案目标动作一些细节。请与目标的行动和细节上要提供的参数值,以便人们能体面提供什么指导的签名更新你的问题。

If you can provide some more details about the target action that you would like to redirect the user to we can provide better answers for you. Please update your question with the signature of the target action and details on what you want to provide as parameter values so people can provide decent guidance.

我猜你想要做的是保存在TempData的一些数据,在目标控制器/动作和过程,从TempData的调用RedirectToAction,负载。

I'm guessing what you want to do is to store some data in TempData, call RedirectToAction, load from TempData in the target Controller/Action and process.

有关TempData的更多信息,请这些问题; http://www.google.com/search?q=tempdata+site %3Astackoverflow.com

For more information on TempData see these questions; http://www.google.com/search?q=tempdata+site%3Astackoverflow.com

这篇关于重定向到其他控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆