65

Is there a way to render inside my view of controller A a partial view from other controller B?

Edit: I wrote a partial view that is good for only two controllers and I don't want to copy it to their both Views folder.
I want The partial view to be displayed each time the View is rendered not after something happens.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 1
    This could mean several different things. Are you just wanting to reuse the view (model generated by controller A)? Are you wanting to invoke the controller action that generates the view? Are you wanting to use AJAX to load the view dynamically after the page is rendered? – tvanfosson Nov 01 '11 at 14:50
  • Can you be more specific. What exactly you want to achieve? – Hari Gillala Nov 01 '11 at 15:06
  • 2
    @Html.Action("YourPartialViewAction", "ControllerName", new { id = Model.id }) – Hari Gillala Nov 01 '11 at 15:08
  • @StewieFG I described some more want I want to achieve. – gdoron Nov 01 '11 at 15:10

5 Answers5

79
  1. You can share views between controllers by putting them into the Views/Shared folder. Each controller can then render that view by name.
  2. You can render a partial view (which can be shared between controllers as in (1)) within the current view using Html.Partial().
  3. You can use Html.Action() to invoke an action on a different controller and render the results within the current view.
  4. You can use AJAX to load a partial view from a different controller after the page has been rendered.
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • About option 3: Can I Invoke my controller and render a partial view from other controller? without using relative path Thanks! – gdoron Nov 01 '11 at 15:02
  • @gdoron you could localize the code that generates the partial view in one or the other controller and include that via `Html.Action()` for views in both controllers. – tvanfosson Nov 01 '11 at 15:13
  • 5
    5.you can also render a view as Partial view by providing the complete path as @Html.Partial("~/Views/Partials/Location.cshtml", Model) – Ishaan Puniani Apr 09 '13 at 19:18
  • but in all these cases, how do you pass the model? – Ayyash Aug 29 '13 at 08:59
  • @Ayyash - in case 1, just like normal, it's just that the view is in a shared location. In case 2, it uses the same model as the "parent" view unless you specify a different one as a parameter. In case 3, you're actually calling an action so you supply a RouteValueDictionary or a map of the route values. Similarly in case 4, you are making a new request, so you add URL parameters or POST from data. In both of the latter cases the view model is passed to the view as normal. – tvanfosson Aug 29 '13 at 13:02
  • oh, u can pass a model in the routevaliedictionary? i didnt know that :/ – Ayyash Aug 29 '13 at 14:18
  • 1
    @Ayyash - no, the action doesn't take a model, it uses the values in the dictionary to populate it's parameters (which may be a model). You don't pass a complete model to it, you invoke it in a view as if you were calling the action method from the web - see http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action.aspx – tvanfosson Aug 29 '13 at 19:00
  • What about calling an Action in controller class? In c# that is? And what about returning a string format of the Action? – Ayyash May 19 '14 at 05:37
45
@Html.Partial("~/Views/ControllerB/Index.cshtml")
Pittfall
  • 2,751
  • 6
  • 32
  • 61
18

Yes,

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

You just need to work out the path part.

Alternatively you can put the partial view in views/shared then just return :

return PartialView("view.cshtml");
Steve
  • 2,988
  • 2
  • 30
  • 47
  • 1
    I don't want the partialview to be in shared. And the relative path doesn't quite appropriate to the mvc routing system. Other suggestions? – gdoron Nov 01 '11 at 14:56
  • Just a small doubt here. Does PartialView method returns the HTML Encoded string or the raw html ? – Karan Feb 09 '17 at 10:54
3
@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>
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Hari Gillala
  • 11,736
  • 18
  • 70
  • 117
2

Just a side note as i found this thread searching for the same question but the answers weren't working: in Orchard CMS modules you cannot use the neat solution posted by Pittfall, you have to use relative paths to return partial views. Lets say you have a controller

Controllers/SiteController.cs

and you want to return the partial view

Shared/MessageList/Items

then in your action methods you need to write

return PartialView("../Shared/MessageList/Items");
Community
  • 1
  • 1
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58