如何使用盒子构建网站

发布于 2024-10-18 02:47:16 字数 254 浏览 4 评论 0原文

我正在设计一个页面,需要在主要内容的左侧和右侧都有框(部分视图)。这些盒子将包含额外的信息,就像 SO 上一样。我想对页面进行分组,因此某些页面将有一些框,而另一组的页面将有其他框。

但我怀疑如何在 MVC.NET 中最好地实现这一点。

我看到两个选项:

1)在每个页面上我都包含我需要的部分控件。

2)创建子母版页,我可以在其中分配页面,并且可以控制应显示哪些部分视图。

还有其他更聪明或更容易实施的方法吗?

I am designing a page which needs to have boxes (partial views) on both the left and the right side of the main content. The boxes will contain extra information just like on SO. I would like to group the pages, so certain pages will have some boxes, and pages of another group will have other boxes.

I am though in doubt how this is best accomplished in MVC.NET.

I see two options:

1) On every page I include the partial controls I need.

2) Create sub-master pages where I can assign a page to, and which can control which partial views should be shown.

Are there any other ways of doing this, which would be smarter or easier to implement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-10-25 02:47:16

SO 页面上的框与页面内容严格相关,因此我将创建一个包含 3 个内容占位符的母版页 (Site.Master):

<asp:ContentPlaceHolder ID="LeftContent" runat="server" />
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<asp:ContentPlaceHolder ID="RightContent" runat="server" />

如果您有一系列具有相同框的功能,我会为以下内容创建母版页:它们并放置类似 (Controller.Master) 的内容:

<asp:Content runat="server" ContentPlaceHolderID="RightContent">
    <% Html.RenderPartial("RelatedTagsInfo", ViewData['RelatedTagsInfoModel']) %>
    <% Html.RenderPartial("RelatedQuestionsInfo", ViewData['RelatedQuestionsModel']) %>
    <asp:ContentPlaceHolder ID="RightContentSubcontent" runat="server" />
</asp:Content>

使用 Controller.Master 的操作会在 RightContentSubcontent 中放置其他框。

如果框确实独立于当前页面,您还可以使用 RenderAction:

<asp:Content runat="server" ContentPlaceHolderID="RightContent">
    <% Html.RenderAction("RelatedTagsInfo", "Question", new { id = Model.Id} ) %>
    <% Html.RenderAction("RelatedQuestionsInfo", "Question", new { id = Model.Id} ) %>
    <asp:ContentPlaceHolder ID="RightContentSubcontent" runat="server" />
</asp:Content>

RenderAction 创建新请求来渲染页面的一部分。它可能更容易使用,但我会使用 RenderAction 来表示不与主视图共享任何信息的框(例如,如果您想显示“时钟”框、“最新下载”框或“登录”框)。

Boxes on SO pages are strictly related to content of the page, so I would create one master page with 3 content placeholders (Site.Master):

<asp:ContentPlaceHolder ID="LeftContent" runat="server" />
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<asp:ContentPlaceHolder ID="RightContent" runat="server" />

If you had some range of functionalities, that have the same boxes, I would create master page for them and place something like (Controller.Master):

<asp:Content runat="server" ContentPlaceHolderID="RightContent">
    <% Html.RenderPartial("RelatedTagsInfo", ViewData['RelatedTagsInfoModel']) %>
    <% Html.RenderPartial("RelatedQuestionsInfo", ViewData['RelatedQuestionsModel']) %>
    <asp:ContentPlaceHolder ID="RightContentSubcontent" runat="server" />
</asp:Content>

Actions using Controller.Master would place additional boxes in RightContentSubcontent.

If boxes are really independent of current page, you can also use RenderAction:

<asp:Content runat="server" ContentPlaceHolderID="RightContent">
    <% Html.RenderAction("RelatedTagsInfo", "Question", new { id = Model.Id} ) %>
    <% Html.RenderAction("RelatedQuestionsInfo", "Question", new { id = Model.Id} ) %>
    <asp:ContentPlaceHolder ID="RightContentSubcontent" runat="server" />
</asp:Content>

RenderAction creates new request to render part of the page. It may be easier to use, but I would use RenderAction for boxes, that do not share any information with the main view (for example if you wanted to display "Clock" box, "Latest downloads" box or "Login" box).

痴者 2024-10-25 02:47:16

将内容放入页面侧边栏的方法有很多种...

  1. 构建不同的母版页,每个母版页包含侧边栏内容的不同子集。我会为此使用 RenderAction 而不是 RenderPartial,这样您就不必担心 MVC 解决方案中的每个 ActionResult 负责将内容发送到这些部分。
  2. 在母版页和 RenderAction 的 ActionResult 中设置单个 RenderAction,让它检查调用控制器和/或 ActionResult(全部包含在 HttpContext 中)。在此基础上,您可以有条件地发回内容,并有条件地在部分视图中渲染它。

There are many ways to put content into sidebars on pages...

  1. Build different master pages, each containing a different subset of sidebar content. I would use RenderAction for this and not RenderPartial, that way you do not have to worry about having each ActionResult in your MVC Solution be responsible for sending content to these partials.
  2. set up a single RenderAction in the Master page and in the ActionResult for the RenderAction, have it inspect the calling Controller and/or the ActionResult (all contained in the HttpContext). Based on that you can send back content conditionally, and render it in the partial view conditionally.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文