MVC redirect with scope parameter

how can we pass parameter and area in redirect redirection

return RedirectToAction("Index","Coupon1", new {Area = "Admin"}, new {id = currentcoupon.Companyid.id}); 
+4
source share
5 answers
 return RedirectToAction("Index", new { id = currentcoupon.Companyid.id, Area="Admin" }); 
+11
source

I don't have enough reputation to add a comment or make changes, so I will just add this as an answer. The marked answer does not provide an adequate explanation of the answer and confuses some people. It assumes a default home controller because it does not explicitly specify a controller and will not work for all situations where another controller is defined for this area. There are many overloads for Redirect, but the easiest way to achieve the stated goal is:

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

Other answers to this post correctly rewrote the source code, but also did not pay attention to the fact that you need to specify the controller in most situations.

An anonymous object acts as a package for mapping MVC routes to match all route values ​​that you can include in your redirects.

+8
source

Just add your parameter to the same object that contains your area.

 return RedirectToAction("Index","Coupon1", new {Area = "Admin", id = currentcoupon.Companyid.id}); 
+5
source

If you need to redirect from a controller registered in a region to a controller without a region, you need to set the region value like this:

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

Without an area = "" you cannot redirect to another controller.

+4
source

If someone is already using a model to transfer data, for example:

 return RedirectToAction("actionName","ControllerName", model); 

you can enable the Area property inside the model and initialize it with any area you want to use,
Example:

 public class ViewModel { public ViewModel() { Area = ""; } public string Area { get; set; } public string OtherProperty { get; set; } } var model = new ViewModel { Area="myArea", OtherProperty="other data" } return RedirectToAction("actionName","ControllerName", model); 
0
source

All Articles