【发布时间】:2016-11-03 16:15:48
【问题描述】:
我有一个困境,我不太确定如何克服。我不知道什么是正确的方法。我正在建立一个网站,并获得了一个模板来与我的服务器代码集成。问题在于模板的轮廓。让我给你举个例子。
<body>
<div class="breakpoint active" id="bp_infinity" data-min-width="588">
<div id="header">full page header content</div>
<div id="body">some stuff</div>
<div id="footer">some stuff</div>
</div>
<div class="breakpoint" id="bp_587" data-min-width="493" data-max-width="587">
<div id="header">mobile header content</div>
<div id="body">some stuff</div>
<div id="footer">some stuff</div>
</div>
<div class="breakpoint" id="bp_492" data-max-width="492">
<div id="header">mobile header content</div>
<div id="body">some stuff</div>
<div id="footer">some stuff</div>
</div>
</body>
我正在尝试以不重复通用代码的方式设置我的 MVC5 视图。我面临的问题是页眉和页脚 div 是页面之间的通用代码,并且正文会发生变化。第二个问题是每个页面都有不同数量的断点。这是第二页来说明我的意思:
<body>
<div class="breakpoint active" id="bp_infinity" data-min-width="588">
<div id="header">full page header content</div>
<div id="body">some stuff</div>
<div id="footer">some stuff</div>
</div>
<div class="breakpoint" id="bp_587" data-max-width="587">
<div id="header">mobile header content</div>
<div id="body">some stuff</div>
<div id="footer">some stuff</div>
</div>
</body>
所以布局页面现在设置起来很棘手,因为我不能只说:
<body>
@RenderBody
</body>
我想到的解决方案之一是使用 Sections,如下所示:
<body>
@RenderBody
@RenderSection("Breakpoint-1", false)
@RenderSection("Breakpoint-2", false)
@RenderSection("Breakpoint-3", false)
</body>
现在每一页都会是:
@section Breakpoint-1
{
<div class="breakpoint active" id="bp_infinity" data-min-width="588">
@{ Html.RenderPartial("full-page-header"); }
@{ Html.RenderPartial("full-page-body"); }
@{ Html.RenderPartial("full-page-footer"); }
</div>
}
@section Breakpoint-2
{
<div class="breakpoint" id="bp_587" data-max-width="587">
@{ Html.RenderPartial("mobile-page-header"); }
@{ Html.RenderPartial("mobile-page-body"); }
@{ Html.RenderPartial("mobile-page-footer"); }
</div>
}
我在上面的代码中看到的一个问题是,如果标题现在需要有 5 个断点而不是 2 个,我需要到处修改它。
有没有更好的方法来做到这一点?我认为最适合我的方案的解决方案是什么?
编辑:澄清。 HTML 中有多个制动点,因为一次只有一个是活动的。当页面达到一定宽度时, 1 当前活动断点被隐藏,新断点变为可见。
【问题讨论】:
-
从最后一段来看,您似乎根据设备显示不同的数据,那么您为什么不只是检测设备/屏幕尺寸并仅渲染您需要的 html(生成 2-5 次为很多 html 只会降低性能)
-
如果你真的想这样做,那么一个选择是创建一个
HtmlHelper扩展方法,可以使用(比如)@Html.Breakpoint(header, body, footer, width)调用,其中前 3 个参数是部分视图(typeofstring)。该方法将输出<div>和部分 -
@StephenMuecke 是一个响应式网站,所以它需要能够在桌面上切换大小。至于您的第二篇文章,这意味着我正在服务器端的字符串中构建我的 HTML。不知何故,感觉不对。
-
是的,我知道 - 但您不需要根据需要向浏览器发送 5 倍的 html - 请参阅 this article。不知道你的第二条评论是什么意思 - 你已经在你的视图中使用
HtmlHelper方法(这些方法在服务器端的字符串中构建 HTML) -
扩展方法意味着你所有的主视图需要渲染输出是
@Html.Breakpoint("full-page-header", "full-page-body", "full-page-footer") @Html.Breakpoint("mobile-page-header", "mobile-page-body", "mobile-page-footer")——我省略了最后一个参数——但它可能应该是一个enum,它允许你有条件地设置属性,比如如id、min-width等
标签: asp.net asp.net-mvc razor asp.net-mvc-5 razor-2