2

I'm working on a page in an Asp.NET MVC 3 application. I have an action in one controller called Alarm. That controller is called Dispatch. For various reasons, I need to modify this action so it displays its results in a view from another controller called Read. This view is called Details.

How can I make the Alarm action return that view? I tried

return View( "Read/Details", vm );

but that displayed this error message:

The view 'Read/Details' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Dispatch/Read/Details.aspx ~/Views/Dispatch/Read/Details.ascx ~/Views/Shared/Read/Details.aspx ~/Views/Shared/Read/Details.ascx ~/Views/Dispatch/Read/Details.cshtml ~/Views/Dispatch/Read/Details.vbhtml ~/Views/Shared/Read/Details.cshtml ~/Views/Shared/Read/Details.vbhtml

What's the right way to do this?

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123

2 Answers2

4

Use full path instead

return View("~/Views/Read/Details.cshtml", vm);
Kalyan
  • 1,395
  • 2
  • 13
  • 26
0

You can redirect to an action on another controller:

return RedirectToAction("ActionName", "ControllerName", routeValues);

Obviously replace the action and controller names with the appropriate values and routeValues will contain whatever parameters the action requires. So if your vm variable is just an integer Id, your code would look something like this:

return RedirectToAction("ActionName", "ControllerName", new { id = 1234 });

Further reading: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx

Troy Carlson
  • 2,965
  • 19
  • 26
  • While the RedirectToAction works, it changes the URL. I want the different view to display but at the current URL. Kalyan's answer, as well as one in the other question, do that. – Tony Vitabile Apr 14 '14 at 13:25
  • Ahh, you're right. I'll leave this answer here in case anyone else is looking for this behavior. – Troy Carlson Apr 24 '14 at 15:46