Question

I am working on a ASP.NET MVC application and have ran in to a strange thing.

I got two controller actions like this:

[CustomAuthorize(Roles = SiteRoles.Admin)]
public ActionResult Review(int? id)

[CustomAuthorize(Roles = SiteRoles.Admin)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Review(AdReview data)

First I call the Review action with null as parameter, this will bring up a webpage with a list of items. The items is linked back to the first Review action with the id set.

When the id have been provided to the review action a edit webpage will be returned for this item. When hitting submit after some changes we will end up on the second Review action (post). Here the item will be saved.

Everything fine so far.

Now, in the last Review action(post) I got the following code at the end :

return RedirectToAction("Review", "Ad");

This will trigger the first Review action again, the problem is that it will provide the previous id? My thought was that RedirectToAction will not provide any parameters?

Was it helpful?

Solution

My thought was that RedirectToAction will not provide any parameters?

Your understanding is wrong. Parameters present in the modelstate are automatically forwarded if the target url contains a route parameter with the same name. In this case you have an id parameter which is being posted (probably as part of an input field or part of the url) and when you redirect back to the original Index action, because your route definitions have an id token at the end the RedirectToAction method will fill it.

As a workaround to avoid this behavior you could explicitly specify that the id parameter should not be sent when redirecting:

return RedirectToAction("Index", "Ad", new { id = "" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top