4

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Check this http://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter . In your case it will looks like: `return RedirectToAction("Review", new { id = "Ad"});` – Samich Sep 11 '11 at 16:15
  • Samich > Your example will generate the url /Controller/Action/Controller in this case /Ad/Review/Ad this does work but the url should just be /Ad/Review ? I have uesed Redirect before and then there have never been any parameter forwarded to the new action? – Banshee Sep 11 '11 at 16:21
  • I can't check it right now. Or specify id to null, like `return RedirectToAction("Review", new { id = null })`? – Samich Sep 11 '11 at 16:27
  • Samich > This will however work : return RedirectToAction("Review", new { id = ""}); but it does not feel right? I have used Redirect before but no parameters have been forwarded? – Banshee Sep 11 '11 at 16:27
  • Samich > Yes I have tried RedirectToAction("Review") and this does not work, the parameter(id) will be forwarded? – Banshee Sep 11 '11 at 16:29
  • Maybe because you already have params in your query string and action name is the same as you just handled – Samich Sep 11 '11 at 16:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3345/discussion-between-samich-and-snowjim) – Samich Sep 11 '11 at 16:30

1 Answers1

4

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 = "" });
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928