ASP.NET MVC – View() vs RedirectToAction() vs Redirect() Methods

In this article, I’m gonna  to discuss View(), RedirectToAction(), and Redirect() in the ASP.NET MVC Application. The ASP.NET MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. Action Results return the result to view the page for the given request.

View() Method :

view is used to display data using the model class object. The Views folder contains all the view files in the ASP.NET MVC application.

Example :

public ActionResult Index() 
{
    UserDetailModel obj = new UserDetailModel(); 
    obj.CountryDetail = new List<Country>() 
    { 
      //......... 
    }; 
    return View(obj);
}

In this above code the view is  (it never call any controller) which is referring the Action Method.
This below also work like above

[HttpPost]
public ActionResult Index(string Name)
{
    return Redirect("ControllerName/ActionName");
}

RedirectToAction() Method :

The RedirectToAction Result in ASP.NET MVC is returning the result to a specified controller and action method. Controller name is optional in RedirectToAction method. If not mentioned, the Controller name redirects to a mentioned action method in the current Controller.

Example :

[HttpPost]
public ActionResult Index()
{
   // return RedirectToAction("Controller", "Action");
   // Or this can be written as
   return RedirectToAction("~/ControllerName/ActionName"); 
}

Redirect() Method :

Redirect also work same as RedirectToAction() and It also makes a new request for the new action , but you have to mention full URL to redirect.

Example :

[HttpPost] 
public ActionResult Index() 
{
   return Redirect("ControllerName/ActionName"); 
}

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories