开发者

How to structure a websitewhen using boxes

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?


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).


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.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜