Home/MVC/Difference between return View() ,return Redirect() and return RedirectToAction() in MVC

Difference between return View() ,return Redirect() and return RedirectToAction() in MVC

return View() :

It commend to generate HTML to display for the required view and render/send it to the browser

Example :

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

return View(obj);
}

In ths above code the view is show the view (it never call any controller) which is refering the Action Method

This below also work like above

[HttpPost]
public ActionResult Index(string Name)
{

READ  Confirmation message after clicking the ActionLink in MVC

return MyIndex();

}

return Redirect()

It commend to redirect to specified URL instead of rendering HTML (ie., it redirect to new URL)

Example

[HttpPost]
public ActionResult Index(string Name)
{
return Redirect(“Home/TIndex”);
}

return RedirectToAction()

It commend to redirect to specified action instead of rendering HTML

Example

[HttpPost]
public ActionResult Index(string Name)
{

return RedirectToAction(“TIndex”);
}

 

READ  Partial View in MVC

Note :

1)  Return View doesn’t make a new requests, it renders the view without changing URLs in the browser’s address bar. (ie., simple like Server.Transfer() )

2)  Return RedirectToAction makes a new requests and URL in the browser’s address bar is changed (ie., simple like Response.Redirect() )

RedirectToAction(“CustomerDetail”, “Customers”, new { CustomerID = 1001 })

In this above,it will generate the URL for you based on your route table.CustomerID will pass to the customerDetail action method uner the
CustomerFolder

3)  Redirect also work same as RedirectToAction , but you have to mention full URL to redirect

 

READ  How to handle Multiple submit buttons in the same form/page

 

One Comment

  1. Hask March 31, 2016 at 7:37 am

    Hi Thiyagu,

    In my site, i have a problem.
    I want use RedirectToAction() but keep before url, can i do it?

    Sorry for my english.

Leave A Comment