View partial view from another controller

Is there a way to visualize inside my view of controller A a partial view from another controller B ?

Edit: I wrote a partial view that is suitable only for two controllers, and I do not want to copy it to both of their views folders.
I want a partial view to be displayed every time the view is not displayed after something happens.

+61
c # asp.net-mvc asp.net-mvc-3 razor partial-views
Nov 01 2018-11-11T00:
source share
5 answers
  • You can exchange views between controllers by placing them in the Views / Shared folder. Then each controller can display this view by name.
  • You can display a partial view (which can be shared between controllers, as in (1)), in the current view using Html.Partial() .
  • You can use Html.Action() to call an action on another controller and display the results in the current view.
  • You can use AJAX to load a partial view from another controller after the page is displayed.
+75
Nov 01 '11 at 2:56 a.m.
source share
 @Html.Partial("~/Views/ControllerB/Index.cshtml") 
+44
Mar 12 '13 at 17:56
source share

Yes,

 return PartialView("/path/view.cshtml"); 

You just need to find part of the way.

Alternatively, you can put a partial view in views / shared, and then just return:

 return PartialView("view.cshtml"); 
+17
Nov 01 2018-11-11T00:
source share
 @model YourModelNamesapce.ModelName @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_LayoutForPartialViews.cshtml"; } <table> <tr> <td> @Html.LabelFor(model => model.fieldname) </td> <td> @Html.DisplayFor(model => model.fieldname) </td> </tr> <tr> <td>@Html.Action("PartialViewAction", "Controller", new { id = Model.id })</td> </tr> </table> 
+1
Nov 01 '11 at 14:55
source share

Just notice that I found this topic to search for the same question, but the answers did not work: in the Orchard CMS modules you cannot use the neat Pittfall solution , you must use relative paths to return partial views. Let's say you have a controller

 Controllers/SiteController.cs 

and you want to return a partial view

 Shared/MessageList/Items 

then in your action methods you need to write

 return PartialView("../Shared/MessageList/Items"); 
+1
Feb 25 '15 at 15:33
source share



All Articles