Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return partial view of another controller by controller?

Tags:

c#

asp.net-mvc

I have an XXX.cshtml file in a Views\ABC folder. Its controller is ABC

I also have an action method in my DEF controller that return a Partialview("XXX" , xyzmodel)

I get a "view not found" error.

How to call that view from other controller

like image 303
Vinit Patel Avatar asked Jan 13 '14 11:01

Vinit Patel


People also ask

How do I return a partial view to another controller?

Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder. And then call it from controller as mentioned above. That's it.

How do I return a view from a controller?

How to return a view from controller action method? To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required.

Can we return partial view from an action?

To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.


1 Answers

Normally the views belong with a specific matching controller that supports its data requirements, or the view belongs in the Views/Shared folder if shared between controllers (hence the name).

"Answer" (but not recommended - see below):

You can refer to views/partial views from another controller, by specifying the full path (including extension) like:

return PartialView("~/views/ABC/XXX.cshtml", zyxmodel); 

or a relative path (no extension), based on the answer by @Max Toro

return PartialView("../ABC/XXX", zyxmodel); 

BUT THIS IS NOT A GOOD IDEA ANYWAY

*Note: These are the only two syntax that work. not ABC\\XXX or ABC/XXX or any other variation as those are all relative paths and do not find a match.

Better Alternatives:

You can use Html.Renderpartial in your view instead, but it requires the extension as well:

Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", modeldata); 

Use @Html.Partial for inline Razor syntax:

@Html.Partial("~/Views/ControllerName/ViewName.cshtml", modeldata) 

You can use the ../controller/view syntax with no extension (again credit to @Max Toro):

@Html.Partial("../ControllerName/ViewName", modeldata) 

Note: Apparently RenderPartial is slightly faster than Partial, but that is not important.

If you want to actually call the other controller, use:

@Html.Action("action", "controller", parameters) 

Recommended solution: @Html.Action

My personal preference is to use @Html.Action as it allows each controller to manage its own views, rather than cross-referencing views from other controllers (which leads to a large spaghetti-like mess).

You would normally pass just the required key values (like any other view) e.g. for your example:

@Html.Action("XXX", "ABC", new {id = model.xyzId }) 

This will execute the ABC.XXX action and render the result in-place. This allows the views and controllers to remain separately self-contained (i.e. reusable).

Update Sep 2014:

I have just hit a situation where I could not use @Html.Action, but needed to create a view path based on a action and controller names. To that end I added this simple View extension method to UrlHelper so you can say return PartialView(Url.View("actionName", "controllerName"), modelData):

public static class UrlHelperExtension {     /// <summary>     /// Return a view path based on an action name and controller name     /// </summary>     /// <param name="url">Context for extension method</param>     /// <param name="action">Action name</param>     /// <param name="controller">Controller name</param>     /// <returns>A string in the form "~/views/{controller}/{action}.cshtml</returns>     public static string View(this UrlHelper url, string action, string controller)     {         return string.Format("~/Views/{1}/{0}.cshtml", action, controller);     } } 
like image 69
Gone Coding Avatar answered Sep 18 '22 04:09

Gone Coding