Friday 28 July 2017

various types of RedirectToAction override methods in ASP.NET MVC

This article overview various types of RedirectToAction override methods used by MVC Developers.

Introduction

In this article, we will try to understand various types of RedirectToAction override methods. Let’s discuss first what RedirectToAction method does and the use of RedirectToAction methods in MVC.  RedirectToAction method returns the specified action on view.  Let us discuss further various types of redirectToAction override methods in MVC.

Background

Lets start understanding the various types of RedirectToAction override methods. First, we will discuss what RedirectToAction method does and the use of RedirectToAction methods in MVC.  RedirectToAction method returns the specified action on view.  Let us discuss further various types of redirectToAction override methods in MVC.
RedirectToAction inherits from System.Web.Mvc.dll. Probably it will use when the developers want to jump from one action to another action methods in the controller.
Example:  We will understand not only technically as well business perspective as well regarding the use of RedirectToAction Method in MVC. Let us discuss business flow of the Banking Corporation and will see an example of that as well. As we know the Banks having the different types of account.  Customers open the one or many accounts in the bank as per their requirement. Here question is how Bank knows about the particular customer. So the Bank having the system to identify the customer with account number. One customer can have many accounts. So here we will see how RedirectToAction method fit to our requirement. 
Now let us discuss technical things. Suppose we have a BankAccount action method and in this method we have implemented for searching the account in Bank. This action method is used to jump to the various types of the action methods in controller level in MVC. So here Developer can move the action in another action of the any account of the customer in the Bank.

In below figure, we are seeing the flow of account searching criteria in Banking. Bank employee searches the customer using account number. Customer gets the information regarding account details. Here we have four action method like BankingAccountSearch, Saving, Salary and Current account. Using RedirectToAction Method communicates each of the account of the customer in Bank.


Types of RedirectToAction Override Methods

Let us see first the various types of RedirectToAction methods which are displayed in below figure. The all below RedirectToAction override methods will be implemented in MVC in controller level. “actionName” is the common parameter of the RedirectToAction method which tells the name of the action in controller.

Let us discuss how RedirectToAction methods interact with MVC namespace.  RedirectToAction methods are accessible from System.Web.Mvc namespace.
We will discuss more on RedirectToAction override method types with an example and code. Most of the time, developers uses the RedirectToAction.  Because it is easy and reliable to call the specified action name in the controller.
 Let us see below various types of RedirectToAction override methods with an examples.
RedirectToAction(string actionName)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with one parameter which is the name of the action. When we call the Index action method then it jumps to the Welcome action name in the current controller.
public ActionResult Index()
        {
            return RedirectToAction("Welcome");
        }
RedirectToAction(string actionName,object routeValue)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with two parameters with the name of the action and object route value. When we call the Index action method then route value calls in Welcome action method in current controller.
public ActionResult Index()
        {
            var date= DateTime.Today;
            return RedirectToAction("Welcome", new  {mydate=date});
        }
RedirectToAction(string actionName,string controllerName)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with two parameters with the name of the action and controller name. When we call the Index action method then it jumps to the Welcome action method in Login controller.
public ActionResult Index()
        {
            var date= DateTime.Today;
            return RedirectToAction("Welcome", "Login");
        }
RedirectToAction(string actionName,RouteValueDictionary  routeValues)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with two parameters with the name of the action name and route value dictionary. When we call the Index action method then Welcome action method calls and routes value loads on respective view.
  public ActionResult Index()
        {
            routeValueDictionary=new System.Web.Routing.RouteValueDictionary();
            routeValueDictionary.Add("Name","jeet:");
            var date= DateTime.Today;
            return RedirectToAction("Welcome", routeValueDictionary);
        }
RedirectToAction(string actionName,string controllerName,object routeValue)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with three parameters with the name of the action, controller name and object route value. When we call the Index action method then Login action method calls in Welcome controller. Object routes value loads on respective view.
public ActionResult Index()
        {
            var date= DateTime.Today;
            return RedirectToAction("Welcome","Login" ,new  {mydate=date});
        }
RedirectToAction(string actionName,string controllerName, RouteValueDictionary  routeValues)
In the below code. RedirectToAction is a return type of action method in controller. RedirectToAction return type overrides the method with three parameters with the name of the action, controller name and route value dictionary. When we call the Index action method then Login action method calls in Welcome controller. routeValue routes on the respective view.
public ActionResult Index()
        {
            RouteValueDictionary routeValueDictionary=new System.Web.Routing.RouteValueDictionary();
            routeValueDictionary.Add("Name","jeet:");
            var date= DateTime.Today;
            return RedirectToAction("Welcome", "Login", routeValueDictionary);
        }

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0