Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC redirecttoaction with parameter with Area

how we can pass parameter and Area in redirecttoaction

return RedirectToAction("Index","Coupon1", 
                          new {Area = "Admin"},
                          new {id = currentcoupon.Companyid.id});
like image 400
Moeez Agha Avatar asked Feb 06 '13 06:02

Moeez Agha


3 Answers

return RedirectToAction("Index", new { id = currentcoupon.Companyid.id, Area="Admin" });
like image 107
Karthik Chintala Avatar answered Nov 17 '22 04:11

Karthik Chintala


I don't have enough reputation to add a comment, or to make a change so I will just add this as an answer. The marked answer does not adequately explain the answer and will confuse some people. It assumes a default home controller since it has not explicitly stated the controller and will not work for all situations when another controller is defined for the area. There are many overloads for the Redirect but the simplest one to achieve the stated goal is of the form:

return RedirectToAction("Action", "Controller", new { id=YourId, Area="AreaName" });

Other answers on this post has rewritten the original code correctly but also did not draw the attention to you needing to specify the controller in most situations.

The anonymous object acts as a bag for the MVC route mapper to map all the route values that you may want to include in your Redirects.

like image 37
Sarel Esterhuizen Avatar answered Nov 17 '22 02:11

Sarel Esterhuizen


If you need to redirect from an controller registered in an area to an controller without area, you have to set the area value to empty like this:

return RedirectToAction("Welcome", "Home", new { Area="" });

Without Area="" you will not be able to redirect to a different controller.

like image 8
Jimpi Avatar answered Nov 17 '22 03:11

Jimpi