当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc 2 - Html.RenderPartial in MasterPage

问题描述:

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"); %>

<!--Footer -->

<% Html.RenderPartial("SiteFooterPartialView"); %>

Then, in the ContentPartialView, after checking the User's Role I finally use the tag:

<asp:ContentPlaceHolder ID="MainContent" runat="server">

</asp:ContentPlaceHolder>

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.

网友答案:

The 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: asp:ContentPlaceHolder with asp:Content tags, Html.RenderPartial and Html.RenderAction/Html.Action helpers.

分享给朋友:
您可能感兴趣的文章:
随机阅读: