开发者

Html.RenderPartial in MasterPage

开发者 https://www.devze.com 2023-02-03 22:12 出处:网络
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

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 t开发者_开发知识库here 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号