Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render partials from another Controller

Tags:

asp.net-mvc

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>
like image 321
Matthew Perron Avatar asked Aug 28 '09 19:08

Matthew Perron


People also ask

How do I return a partial view to another controller?

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.

What is render partial rails?

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.

How can you tell Rails to render without a layout?

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.


1 Answers

Use the Html.RenderAction method from the ASP.NET MVC Futures library.

like image 177
Adrian Grigore Avatar answered Sep 27 '22 20:09

Adrian Grigore