基于模型和视图内容限制 ASP.NET MVC 2 母版页上的 JavaScript 和 CSS 文件

发布于 2024-09-02 00:55:32 字数 1548 浏览 6 评论 0原文

我想仅在需要的页面上包含某些 .js 和 .css 文件

例如,我的 EditorTemplate DateTime.ascx 需要文件 anytimec.jsanytimec.css

每当我使用 EditorFor< 时,都会应用该模板/a> 或 EditorForModel 辅助方法在具有 DateTime 类型值的模型的视图中。

我的技术:

我已将此条件放入母版页的 部分中。它检查 模型元数据

<% if (this.ViewData.ModelMetadata.Properties.Any(p => p.ModelType == typeof(DateTime))) { %>
    <link href="../../Content/anytimec.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/anytimec.js" type="text/javascript"></script>
<% } %>

这有两个问题

  1. 如果我嵌套了 DateTime 类型的子模型,则失败

  2. 由没有 EditorForEditorForModel 方法(示例:DisplayForModel)

如何改进这项技术?

I want to include certain .js and .css files only on pages that need them.

For example, my EditorTemplate DateTime.ascx needs files anytimec.js and anytimec.css.

That template is applied whenever I use either the EditorFor or EditorForModel helper methods in a view for a model with a DateTime type value.

My technique:

I've put this condition into the <head> section of my master page. It checks for a DateTime type property in the ModelMetadata.

<% if (this.ViewData.ModelMetadata.Properties.Any(p => p.ModelType == typeof(DateTime))) { %>
    <link href="../../Content/anytimec.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/anytimec.js" type="text/javascript"></script>
<% } %>

This has two problems:

  1. Fails if I have nested child models of type DateTime

  2. Unnecessarily triggered by views without EditorFor or EditorForModel methods (example: DisplayForModel)

How can I improve this technique?

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

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

发布评论

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

评论(2

尹雨沫 2024-09-09 00:55:32

就个人而言,我会使用部分视图和 ASP 内容占位符。

在视图/共享中有一个部分视图EditorScripts.ascx,其中包含定义要包含在编辑页面中的脚本的标签。

然后在 Site.Master 标记中放置一个占位符,如下所示:

<asp:ContentPlaceHolder ID="HeadContent" runat="server" />

在您想要/需要脚本的任何视图中,放置此代码:

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <% Html.RenderPartial("EditorScripts");  %>
</asp:Content>

这不是一个完美的解决方案并且没有您想要的那么动态。使用部分视图的原因是,如果您决定更新或向这些视图添加更多脚本,则只需在一个位置进行更新。

另一种方法是拥有一个包含这些脚本和其他编辑器特定内容的 Editor.Master 页面,然后让该母版使用 Site.Master 作为其母版页。然后,所有编辑器视图都将 Editor.Master 作为其母版页。

华泰

Personally, I would use a partial view and an ASP Content Placeholder.

In the Views/Shared have a partial view EditorScripts.ascx which contains the tags defining the scripts to be included in your editing pages.

Then put a placeholder in the Site.Master <head> tag, something like this:

<asp:ContentPlaceHolder ID="HeadContent" runat="server" />

The, in any view that you want/need the scripts, put this code:

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <% Html.RenderPartial("EditorScripts");  %>
</asp:Content>

This isn't a perfect solution and isn't as dynamic as you would like. The reason for using a partial view is that if you decide to update or add more scripts to those views, then you only have to update it in one location.

Another way would be to have a Editor.Master page that contains these scripts and other editor specific things then have that master use the Site.Master as it's master page. Then all editor views would have the Editor.Master as their master page.

HTH

梦在深巷 2024-09-09 00:55:32

我认为 telerik Web 资产管理器 允许您完成你想要的并且是开源的。

I think the telerik web asset manager allows you to accomplish what you want and is open source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文