In most of the Actions in my controller I need to check to see if a condition has been meet, and if not redirect the user to another Action in the same controller so I tried:
public ActionResult Transactions()
{
GrossGalsConfermation();
return View();
}
public void GrossGalsConfermation()
{
if (Session["HasConfirmed"] == null && TerminalUserData.IsGrossGallonTerminal)
{
RedirectToAction("ConversionFactors");
}
}
but even when the if
statement is true the RedirectToAction
is not executed. I have done a debug in VS and can see it jump from the Action
to the Method
, evaluate the IF
statement and go into it to run the RedirectToAction
but then it jumps right back to the calling Acation
and return's it's view. How can I force it to redirect? Searching on line I find similar problems and the solution was to change from void
to ActionResult
but then I have to provide an fallback Action
to return which can not be done since this Method
will be called by dozens of views.
To redirect the user to another action method from the controller action method, we can use RedirectToAction method.
RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Save this answer. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .
You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.
An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.
All RedirectToAction
does is return a RedirectToRouteResult
- the browser won't be redirected unless you actually return it. Since you don't want to do this within your method (you could return null and do null checks in your action but it would be clumsy), I'd suggest using an action filter instead. Something like the following would do it:
public class GrossGalsFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Session["HasConfirmed"] == null
&& TerminalUserData.IsGrossGallonTerminal)
{
filterContext.Result =
new RedirectToRouteResult(new RouteValueDictionary(new
{ controller = "MyController", action = "ConversionFactors" }));
}
base.OnActionExecuting(filterContext);
}
}
Then decorate your controller actions:
[GrossGalsFilter]
public ActionResult MyControllerAction
Note that you may have to modify the filter to get hold of TerminalUserData.IsGrossGallonTerminal
- impossible to know where this comes from based on your question.
More on action filters: http://msdn.microsoft.com/en-us/library/dd381609(v=vs.100).aspx
You are missing a "return":
return RedirectToAction(...);
If you want to reuse the method multiple times you should look into writing a filter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With