tags:

views:

1661

answers:

4

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:

[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.

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:

    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.

+2  A: 

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.

tvanfosson
+2  A: 

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

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

Ariel Popovsky
A: 

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.

Marc Climent
A: 

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: http://stackoverflow.com/questions/879608/asp-net-mvc-controller-parameter-not-being-gathered-from-form

burnside