基于Model和View含量对ASP.NET MVC 2的母版页限制JavaScript和CSS文件 [英] Limit JavaScript and CSS files on ASP.NET MVC 2 Master Page based on Model and View content

查看:174
本文介绍了基于Model和View含量对ASP.NET MVC 2的母版页限制JavaScript和CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括某些的.js和.css文件只在需要它们的网页

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

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

每当我请使用的 EditorFor EditorForModel 辅助方法在一个DateTime类型值模型的视图。

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.

我的技巧:

我已经把这个条件到<头>我母版页部分。它作为一个DateTime 键入的 ModelMetadata

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>
<% } %>

这有两个问题


  1. 失败,如果我有嵌套DateTime类型的儿童模特

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

不必要地看法,并没有触发EditorFor EditorForModel 方法(例如: DisplayForModel

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

如何改进这项技术?

推荐答案

我觉得的 Telerik的网络资产管理可以让你完成你想要什么,是开源的。

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

这篇关于基于Model和View含量对ASP.NET MVC 2的母版页限制JavaScript和CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆