I tried to organize a messy MasterPage from a Project I have to work on by using RenderPartials since its code was truly unreadable. Depending on user role the main content data is displayed in a way or the other. I did this:
<!-- Header -->
<% Html.RenderPartial("SiteHeaderPartialView"); %>
<!-- Content -->
<% Html.RenderPartial("ContentPartialView"); %>
<% Html.RenderPartial("SiteFooterPartialView"); %>
Then, in the ContentPartialView, after checking the User's Role I finally use the tag:
<asp:ContentPlaceHolder ID="MainContent" runat="server">
But when the site renders I get the error message: "Cannot find ContentPlaceHolder 'MainContent' in the master page". So, my question is this, Is it possible to use RenderPartials in the MasterPage and put the ASP ContentHolders inside them? Why is that? Is there another way to reorganize the MasterPage so I can make it look cleaner and more readable? Thank you in advance for your advices.
asp:ContentPlaceHolder tag should be used in the master page. In the content pages you need to use
asp:Content. But with
Html.RenderPartial you cannot use them, it simply inserts a partial ASCX page at the indicated location. So here are three common ways to compose layouts:
Html.RenderPartial and Html.RenderAction/Html.Action helpers.