If I have a HomeController displaying its Index view, how would I proceed in order to have the Index view imbed a UserControl from another Controller?
Here's a look at the content of the Home/Index View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
<%=Resources.Global.HomeTitle %>
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p><%=Resources.Global.HomeIndex %></p>
<h3>Partial title</h3>
<% Html.RenderPartial("~/Views/OtherController/SomeAction.ascx"); %>
</asp:Content>
Here's the OtherController content:
public class OtherController : BaseController
{
private readonly IRepositoryContract<SomeType> repo = new SomeTypeRepository();
public ActionResult SomeAction()
{
IQueryable<SomeType> items = repo.GetAllItems();
return View("SomeAction", items);
}
}
This gives me an NullReferenceException since the Controller is never being called by the RenderPartial() method. Changing the following line
<% Html.RenderPartial("~/Views/OtherController/SomeAction.ascx"); %>
by this
<% Html.RenderPartial("~/Views/OtherController/SomeAction.ascx",((ViewResult) new OtherController().SomeAction()).ViewData.Model); %>
works, but it sure is ugly as hell. There has to be a better way to imbed partials from another controller?
Update :: Solution found
Here's the code after implementing Adrian Grigore's solution:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Microsoft.Web.Mvc"%>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
<%=Resources.Global.HomeTitle %>
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p><%=Resources.Global.HomeIndex %></p>
<h3>Partial title</h3>
<% Html.RenderAction("SomeAction","OtherController"); %>
</asp:Content>
How to return a partial view from controller action method? To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.
Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.
Use the Html.RenderAction method from the ASP.NET MVC Futures library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With