仅当 Javascript 尚未包含在 .NET MVC 中时才包含 Javascript
在 .NET MVC 部分视图中,我想做这样的事情:
<% ScriptCollection.RequireScript("path/to/script.js"); %>
这需要 ScriptCollection 对象中的一个方法,如下所示:
public void RequireScript(string src)
{
if (!_List.Contains(src))
_List.Add(src);
}
然后,在我的母版页中,我会有一些东西像这样:
<html>
<head></head>
<body>
<!-- Content would go here -->
<% foreach (var script in ScriptCollection) { %>
<script type="text/javascript" src="<%= script %>"></script>
<% } %>
</body>
</html>
我的问题是这样的: 如何使 ScriptCollection 对象可通过部分视图进行更新并可供母版页使用?
编辑: 我不想在控制器中添加所需的脚本,也不想使用强类型母版页。如果这些方法被认为是最佳实践,那么欢迎提出这样做的建议。
编辑#2: 使用扩展属性这会很容易。我可以为 HtmlHelper
类提供 ScriptCollection
属性。我是否错过了类似已经存在的东西?
In a .NET MVC partial view, I'd like to do something like this:
<% ScriptCollection.RequireScript("path/to/script.js"); %>
That would require a method in the ScriptCollection
object that looks like this:
public void RequireScript(string src)
{
if (!_List.Contains(src))
_List.Add(src);
}
Then, in my Master page, I would have something like this:
<html>
<head></head>
<body>
<!-- Content would go here -->
<% foreach (var script in ScriptCollection) { %>
<script type="text/javascript" src="<%= script %>"></script>
<% } %>
</body>
</html>
My question is this:
How can I make the ScriptCollection
object available to be updated by a partial view and also available to be used by a Master page?
Edit:
I do not want to add the required scripts in a controller, nor do I want to use a strongly typed Master page. Though suggestions on doing so are welcome if those methods are considered a best practice.
Edit #2:
This would be easy with extension properties. I could just give the HtmlHelper
class the ScriptCollection
property. Am I missing something like this that already exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我检查了 Telerik 组件。对于我的需要来说,它们有点过分了。另外,开源许可证 (GPL v2) 不是我正在寻找的类型。
我启动了一个根据 MIT 开源许可证获得许可的项目,并将其托管在 Google Code 上。这是解决我的问题的轻量级解决方案。
code.google.com/p/script-keeper
I checked out the Telerik assemblies. They're a little overkill for what I need. Also, the open-source license (GPL v2) isn't the type I'm looking for.
I started a project licensed under the MIT open-source license and am hosting it on Google Code. It's a lightweight solution to my problem.
code.google.com/p/script-keeper
您可以查看 Telerik 的 ScriptRegisterBuilder 类实现完全符合您的要求。或者你也可以直接使用它。此外,它还支持:
现在,即使您不使用 Telerik 提供的任何其他内容,您也可以使用 ScriptRegisterBuilder 类,但建议禁用自动 jquery 和 ScriptRegisterBuilder 类。 jquery验证脚本注册。另请参阅提供的链接。
另请查看
StyleSheetRegistrarBuilder
类。这是 ScriptRegisterBuilder 类的侄子,负责处理 CSS 资源。我确实注意到您问题中的 MVC2 标签,但我的简单示例使用 razor,只是为了说明这一点。让它也适用于 MVC2/ASPX 应该不难。否则请告诉我。
主视图
局部视图
You could take a look at Telerik's ScriptRegisterBuilder class implementation which does exactly what you want. Or you can just use it as well. In addition it also has support for:
Now even when you don't use any of the other stuff Telerik provides you can use the
ScriptRegisterBuilder
class but then it's advised to disable the automatic jquery & jquery validation script registration. See also the link provided.Also take a look the
StyleSheetRegistrarBuilder
class. This is the nephew of theScriptRegisterBuilder
class and handles the CSS assets.I did noticed the MVC2 tag in your question but my simple example uses razor and is just for making the point. It shouldn't be hard to make it work for MVC2/ASPX as well. Otherwise let me know.
Master
Partial View