문제

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?

도움이 되었습니까?

해결책

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 = "" });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top