I have a shared view located in Shared/Header.aspx and I want to render this as part of the HeaderContent ContentPlaceHolder. My master page contains:
<asp:ContentPlaceHolder ID="HeaderContent" runat="server" />
and I want to be able to tell MVC to populate that content place holder with the Shared/Header view, which contains:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderContent" runat="server">
....
</asp:Content>
At the moment in each of the pages where I want the HeaderContent to be populated (and on some pages I don’t want it to be) I do this:
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">
<% Html.RenderPartial("Header"); %>
</asp:Content>
Is there any way I can effectively do the above but from the Controller handling the request (or a child class of ViewPage)?
If you want to do this from the controller, then you could pass a property in the ViewData, which could be picked up from the layout page. E.g. in a controller action:
Then in the layout page you could say
That way you don’t need the extra content placeholder too.