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请求次数,提高网络加载速度和页面 ...
随机推荐
- 苹果iOS锁屏制作
下面我们开始. 一.锁屏界面 可以观察到,iphone的锁屏界面在时间和解锁部分有着透明强高光风格的背景,高光部分有非常明显的界限,边缘部分1像素的高光也是非常醒目的,整体感觉整个表面非常光滑,如同玻 ...
- 允许ubuntu下mysql远程连接
第一步: gedit /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind- ...
- opencv 手势识别
我使用OpenCV2.4.4的windows版本+Qt4.8.3+VS2010的编译器做了一个手势识别的小程序. 本程序主要使到了Opencv的特征训练库和最基本的图像处理的知识,包括肤色检测等等. ...
- 2015南阳CCPC C - The Battle of Chibi DP
C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...
- Oracle 生成随机密码
需求:需要定期更改密码.要求是1.密码位数11位.2.必须包含大小写字母.数字.特殊字符.3.排除一些特殊字符如().@.& oracle数据库中有可已生成随机密码包dbms_random,但 ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- 【javaSE】HashSet和HashMap
************************************************************************ ****原文:blog.csdn.net/clar ...
- pt-online-schema-change原理解析 博客相关需要阅读
xiaoboluo768 http://www.lai18.com/user/481193.html 都说pt-toolkit工具集中的pt-online-schema-change可以在线不锁 ...
- VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测
原文链接:http://bbs.weiphone.com/read-htm-tid-7625465.html 建议电脑要求 Windows 7/8, 32 / 64 bit CPU Int ...
- Qt... configure: error: Qt (>= Qt 2.2.2) (headers…
转载:http://blog.chinaunix.net/uid-23733724-id-290980.html 昨天开始在自己的fedora12下装qt~ 但是按照教程在/opt/Embed ...