MasterPage 中的 Html.RenderPartial
我尝试使用 RenderPartials 从我必须处理的项目中组织一个混乱的 MasterPage,因为它的代码确实不可读。根据用户角色,主要内容数据以某种方式显示。我这样做了:
<!-- Header -->
<% Html.RenderPartial("SiteHeaderPartialView"); %>
<!-- Content -->
<% Html.RenderPartial("ContentPartialView"); %>
<!--Footer -->
<% Html.RenderPartial("SiteFooterPartialView"); %>
然后,在 ContentPartialView 中,检查用户角色后,我最终使用标签:
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
但是当站点呈现时,我收到错误消息:“在母版页中找不到 ContentPlaceHolder 'MainContent'”。所以,我的问题是,是否可以在 MasterPage 中使用 RenderPartials 并将 ASP ContentHolders 放入其中?这是为什么?是否有另一种方法可以重新组织母版页,使其看起来更干净、更具可读性?预先感谢您的建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应在母版页中使用
asp:ContentPlaceHolder
标记。在内容页面中,您需要使用asp:Content
。但对于Html.RenderPartial
您无法使用它们,它只是在指定位置插入部分 ASCX 页面。因此,以下是三种常见的布局布局方式:带有asp:Content
标记的asp:ContentPlaceHolder
、Html.RenderPartial
和 Html.RenderAction/Html.Action 帮助器。The
asp:ContentPlaceHolder
tag should be used in the master page. In the content pages you need to useasp:Content
. But withHtml.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
withasp:Content
tags,Html.RenderPartial
and Html.RenderAction/Html.Action helpers.