ASP.NET MVC Partial页输出JS
很多情况Partial是需要引用到JS的,通常做法是吧JS在引用Partial的页面中加入JS文件或者JS代码。
前阵子网上看到一段代码可以在Partial页面中添加JS,输出道引用页面。
public static class HtmlExtensions
{
private const string JscriptDeferRazorViewdata = "__jsdfrz";
private const string JscriptIncludeViewdata = "__jsrq"; public static void DeferScript(this HtmlHelper html, string scriptLocation)
{
string jsTag = "<script type=\"text/javascript\" src=\"" + scriptLocation + "\"></script>"; var jscripts = html.ViewContext.TempData[JscriptIncludeViewdata] as List<string> ?? new List<string>();
if (!jscripts.Contains(jsTag))
{
jscripts.Add(jsTag);
}
html.ViewContext.TempData[JscriptIncludeViewdata] = jscripts;
} public static MvcHtmlString Script(this HtmlHelper html, Func<int, HelperResult> script)
{
var jsActions = html.ViewContext.TempData[JscriptDeferRazorViewdata] as List<Func<int, HelperResult>> ?? new List<Func<int, HelperResult>>();
jsActions.Add(script);
html.ViewContext.TempData[JscriptDeferRazorViewdata] = jsActions;
return MvcHtmlString.Empty;
} public static IHtmlString RenderScripts(this HtmlHelper html)
{
var jscripts = html.ViewContext.TempData[JscriptIncludeViewdata] as List<string>;
var jsActions = html.ViewContext.TempData[JscriptDeferRazorViewdata] as List<Func<int, HelperResult>>;
html.ViewContext.TempData[JscriptIncludeViewdata] = new List<string>();
html.ViewContext.TempData[JscriptDeferRazorViewdata] = new List<Func<int, HelperResult>>();
return new HelperResult(writer =>
{
if (jscripts != null)
{
writer.Write(string.Join("\r\n", jscripts.ToArray()));
}
if (jsActions != null) foreach (var action in jsActions) { action().WriteTo(writer); }
});
}
}
在引用Partial的页面中添加:
@section scripts{
@Html.RenderScripts()
}
Partial页面:
@{
Html.Script(
@<script type="text/javascript">
//再此些你的JS代码
</script>
);
Html.DeferScript("//再此引用你的JS文件");
}
ASP.NET MVC Partial页输出JS的更多相关文章
- ASP.NET MVC 4 RC的JS/CSS打包压缩功能 (转载)
ASP.NET MVC 4 RC的JS/CSS打包压缩功能 打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载 ...
- ASP.NET MVC controller 之间传JS值
在ASP.NET MVC中有东西叫TempData,它的类型是TempDataDictionary,它与ViewData以及ViewBag的不同之处在于 它的内部是使用session来保存信息的,可以 ...
- ASP.NET MVC 4 RC的JS/CSS打包压缩功能 Scripts.Render和Styles.Render
打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...
- Asp.net中向前端输出JS的一些调用
最近突然写ASP.NET项目,用到向前台输出JS脚本,但是以前在MVC里是通过异步或者一些方法来调用,但是ASP.net用到的很少.在网上找到一个HELPER.CS.保存一下,以后再用. using ...
- (转)ASP.NET MVC 4 RC的JS/CSS打包压缩功能
转自:http://www.cnblogs.com/shanyou/archive/2012/06/22/2558580.html 打包(Bundling)及压缩(Minification)指的是将多 ...
- ASP.NET MVC 4 RC的JS/CSS打包压缩功能
打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...
- 【转】ASP.NET MVC 4 RC的JS/CSS打包压缩功能
原文链接:http://www.cnblogs.com/shanyou/archive/2012/06/22/2558580.html 打包(Bundling)及压缩(Minification)指的是 ...
- asp.net错误页和asp.net mvc错误页设置
asp.net错误页 在日常项目开发过程中,我们需要给网站设置错误页和记录错误日志. 首先,在项目中添加全局应用程序类 在Global.asax中 protected void Application ...
- ASP.NET MVC 4 Optimization的JS/CSS文件动态合并及压缩
JS/CSS文件的打包合并(Bundling)及压缩(Minification)是指将多个JS或CSS文件打包合并成一个文件,并在网站发布之后进行压缩,从而减少HTTP请求次数,提高网络加载速度和页面 ...
随机推荐
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Sql CLR
using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsof ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
- ORA-01033: ORACLE initialization or shutdown in progress 实用的处理方法
ORA-01033: ORACLE initialization or shutdown in progress 实用的处理方法,此问题通常是由于电脑非正常关机造成的,我们可以用下面的方法查找出是那个 ...
- Spring Batch Concepts Chapter
Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...
- linux虚拟主机wdcp系列教程之四
当我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习. 1.wdcp支持的在线解压 有时小文件数据量,但整个目录上传会比较慢,如果打包 ...
- Enhancing the Scalability of Memcached
原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...
- heritrix
Heritrix3.0教程 http://blog.csdn.net/neo_liukun/article/category/1118819
- stl lower_bound upper_bound binary_search equal_range
自己按照stl实现了一个: http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...
- iOS开发笔记系列-基础7(C语言特性)
Objective-C是C语言的扩展,因此,也具备很多C语言的基本特性,这里只罗列部分. 块(Blocks) 块是对C语言的一种扩展,它并未作为标准ANSI C所定义的部分,而是Apple添加到语言中 ...