基于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
问题描述
我想包括某些的.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 A>。
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>
<% } %>
这有两个问题
-
失败,如果我有嵌套DateTime类型的儿童模特
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屋!