Hcabnettek
Hcabnettek

Reputation: 12928

ASP.NET MVC How do I point a controller action to a different view?

I have a controller with a method that points to a view. How do I change the view that the action is mapped to? Like I want it to call ViewB instead of ViewA? Where do these mappings exist and how can I modify them? Thanks for any tips.

Thanks,
~ck in San Diego

Upvotes: 4

Views: 3354

Answers (5)

Johannes Setiabudi
Johannes Setiabudi

Reputation: 2077

You can do the default:

return View(myModel);

Or specify the View Name under the same controller view folders or in shared:

return View("ThatView", myModel);

Or whatever view:

return View("~/myfolder/WhatEverView.ascx", myModel);

Upvotes: 1

Jake Kalstad
Jake Kalstad

Reputation: 2065

You could also return a RedirectToAction("View"), or with Javascript

 json(new { Redirect = url.Action(action, data) }, JsonRequestBehavior.AllowGet);

and handle the return appropriately on the client side.

Happy Hunting!

Upvotes: 2

adw
adw

Reputation: 5021

You can pass the view name into the View method: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx

Upvotes: 1

John Farrell
John Farrell

Reputation: 24754

Instead of:

return View(someModel);

use

return View("ViewYouWant", someModel);

Upvotes: 8

p.campbell
p.campbell

Reputation: 100567

To have a controller method redirect to a view that's not named the same as the action method, you can change the statement from

 return View();

to

 return View("ViewB");

Upvotes: 7

Related Questions