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请求次数,提高网络加载速度和页面 ...
随机推荐
- mime type 概要介绍
内容类型 内容类型(Content-Type),这个头部领域用于指定消息的类型.一般以下面的形式出现. Content-Type: [type]/[subtype]; parameter type t ...
- [Windows驱动开发](一)序言
笔者学习驱动编程是从两本书入门的.它们分别是<寒江独钓——内核安全编程>和<Windows驱动开发技术详解>.两本书分别从不同的角度介绍了驱动程序的制作方法. 在我理解,驱动程 ...
- JQuery要点(一)
简介: jQuery Mobile 是用于创建移动 Web 应用的前端开发框架. jQuery Mobile 可以应用于智能手机与平板电脑. jQuery Mobile 使用 HTML5 & ...
- 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
(26)Don’t underestimate the value of the UI when tackling performance problems 招数26: 解决性能问题时不要低估UI的价 ...
- JavaScrip基础讲座 - 神奇的ProtoType
1. 什么是 prototype prototype 对于 JavaScript 的 意义重大,prototype 不仅仅是一种管理对象继承的机制,更是一种出色的设计思想 在现实生活中,我们常常说, ...
- utf-8 和gbk编码的差别
UTF- 8: 是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码. GBK 是国家标准GB2312基础上扩容后兼容GB2312的标准. GBK的文 ...
- linux对外开放某个端口命令
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /etc/rc.d/init.d/iptables save /etc/rc.d/init.d/ ...
- javascript常用方法整理--数组篇
1. arrayObject.slice(start,end) 从已有的数组中返回选定的元素 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说 ...
- npm获取配置,设置代理
npm获取配置有6种方式,优先级由高到底. 命令行参数. --proxy http://server:port即将proxy的值设为http://server:port. 环境变量. 以npm_con ...
- Jquery_异步上传文件多种方式归纳
1.不用任何插件,利用iframe,将form的taget设为iframe的name,注意设为iframe的id是没用的,跟网上很多说的不太一致 iframe_upload.htm <!DOCT ...