ASP.NET MVC RedirectToAction传递错误的参数值?

时间:2021-12-13 03:27:12

I am using ASP.NET MVC 1.0. I have an ActionResult which receives a form post with a value from a drop down list. It then redirects to an ActionResult, passing the value as a parameter. Here are my 2 ActionResult methods:

我正在使用ASP.NET MVC 1.0。我有一个ActionResult,它从下拉列表中接收一个带有值的表单帖子。然后它重定向到ActionResult,将值作为参数传递。这是我的2个ActionResult方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FindPromo()
{
    var promoId = Convert.ToInt32(Request.Form["ddlPromotion"]);
    return RedirectToAction("GetPromo", new { id= promoId });
}

public ActionResult GetPromo(int id)
{

    if (promoId > 0)
    {
        var model = GetModel(id);

        if (model.Promo != null)
        {
            return View("Promo", GetModel(id));
        }
    }

    return RedirectToAction("NotFound");
}

When I debug this, the value from the drop down list is pulled correctly from my form, but on the redirect, a different value from a previous request comes through as the promoId value on the GetPromo method. This happens even if I I totally close out of the page and reload it, almost as if it's cached somewhere. I know this is something simple, but I've never run across this before.

当我调试它时,从我的表单中正确地提取下拉列表中的值,但在重定向上,与先前请求不同的值作为GetPromo方法上的promoId值。即使我完全关闭页面并重新加载它,就好像它被缓存在某个地方一样。我知道这很简单,但我以前从未碰过这个。

EDIT:

I changed the parameter names from promoId to id, and also updated my routing table. I've updated my code sample above, and here are my routes:

我将参数名称从promoId更改为id,并更新了我的路由表。我已经更新了上面的代码示例,这是我的路线:

    routes.MapRoute("GetPromo", "{controller}/GetPromo/{id}",
        new { controller = "Promo", action = "GetPromo" });

    routes.MapRoute("FindPromo", "{controller}/FindPromo/{id}",
        new { controller = "Promo", action = "FindPromo" });

I'm still getting the same results with the parameters, however.

然而,我仍然使用参数获得相同的结果。

4 个解决方案

#1


Have you tried using the default route setting: {controller}/{action}/{id}? Also, are those your only routes? The routes are evaluated in sequence so a previous route can override one below if it matches. Phil Haack published a route debugger in his blog at haacked.com

您是否尝试过使用默认路由设置:{controller} / {action} / {id}?那些是你唯一的路线吗?路由按顺序进行评估,因此如果匹配,则先前路由可以覆盖下面的路径。 Phil Haack在他的博客haacked.com上发布了一个路由调试器

You can also play with: RouteTable.Routes.GetVirtualPath(), passing the ViewContext.RequestContext and a RouteValueDictionary and see the resulting Url.

您还可以使用:RouteTable.Routes.GetVirtualPath(),传递ViewContext.RequestContext和RouteValueDictionary,并查看生成的Url。

#2


I wonder if it is related to your routing set up. Try renaming promoId to id and updating your route values. If that doesn't work, could you update your post with your routes.

我想知道它是否与您的路由设置有关。尝试将promoId重命名为id并更新路由值。如果这不起作用,您可以使用您的路线更新您的帖子。

#3


Have you tried this?

你试过这个吗?

return RedirectToAction("GetPromo", new { id = promoId.ToString() });

I think we ran on a similar problem and that solved the issue.

我认为我们遇到了类似的问题并解决了这个问题。

If not, you can do this:

如果没有,你可以这样做:

var dict = new RouteValueDictionary();
dict.Add("id", promoId);

return RedirectToAction("GetPromo", dict);

And as a recommendation, you shouldn't use Convert.ToInt32, use int.Parse or int.TryParse instead.

作为建议,您不应该使用Convert.ToInt32,而是使用int.Parse或int.TryParse。

#4


Check that your IOC container isn't registering your controllers as singletons. I'm using Windsor and the default lifestyle type is singleton and this was causing the "caching" behaviour. See: ASP.NET MVC - Controller parameter not being gathered from form?

检查您的IOC容器是否未将控制器注册为单件。我正在使用Windsor,默认的生活方式类型是单身,这导致了“缓存”行为。请参阅:ASP.NET MVC - 未从表单收集控制器参数?

#1


Have you tried using the default route setting: {controller}/{action}/{id}? Also, are those your only routes? The routes are evaluated in sequence so a previous route can override one below if it matches. Phil Haack published a route debugger in his blog at haacked.com

您是否尝试过使用默认路由设置:{controller} / {action} / {id}?那些是你唯一的路线吗?路由按顺序进行评估,因此如果匹配,则先前路由可以覆盖下面的路径。 Phil Haack在他的博客haacked.com上发布了一个路由调试器

You can also play with: RouteTable.Routes.GetVirtualPath(), passing the ViewContext.RequestContext and a RouteValueDictionary and see the resulting Url.

您还可以使用:RouteTable.Routes.GetVirtualPath(),传递ViewContext.RequestContext和RouteValueDictionary,并查看生成的Url。

#2


I wonder if it is related to your routing set up. Try renaming promoId to id and updating your route values. If that doesn't work, could you update your post with your routes.

我想知道它是否与您的路由设置有关。尝试将promoId重命名为id并更新路由值。如果这不起作用,您可以使用您的路线更新您的帖子。

#3


Have you tried this?

你试过这个吗?

return RedirectToAction("GetPromo", new { id = promoId.ToString() });

I think we ran on a similar problem and that solved the issue.

我认为我们遇到了类似的问题并解决了这个问题。

If not, you can do this:

如果没有,你可以这样做:

var dict = new RouteValueDictionary();
dict.Add("id", promoId);

return RedirectToAction("GetPromo", dict);

And as a recommendation, you shouldn't use Convert.ToInt32, use int.Parse or int.TryParse instead.

作为建议,您不应该使用Convert.ToInt32,而是使用int.Parse或int.TryParse。

#4


Check that your IOC container isn't registering your controllers as singletons. I'm using Windsor and the default lifestyle type is singleton and this was causing the "caching" behaviour. See: ASP.NET MVC - Controller parameter not being gathered from form?

检查您的IOC容器是否未将控制器注册为单件。我正在使用Windsor,默认的生活方式类型是单身,这导致了“缓存”行为。请参阅:ASP.NET MVC - 未从表单收集控制器参数?