zoukankan      html  css  js  c++  java
  • 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(0).WriteTo(writer); }
                });
            }
        }

    在引用Partial的页面中添加:

    @section scripts{
        @Html.RenderScripts()
    }

    Partial页面:

    @{
        Html.Script(
            @<script type="text/javascript">
              //再此些你的JS代码
             </script>
            );
       Html.DeferScript("//再此引用你的JS文件");
    }
    作者:guozx 出处:http://www.cnblogs.com/Innovate/ 声明:原创文字只代表本人某一时间内的观点或结论,本人不对涉及到的任何代码担保。转载请标明出处!
  • 相关阅读:
    Linux命令发送Http的get或post请求(curl和wget两种方法)
    大数据面试题以及答案整理(一)
    大数据面试题及答案-汇总版
    Linux shell之打印输出
    Java开发中常见的危险信号(上)
    sencha touch笔记(5)——DataView组件(1)
    sencha touch(7)——list组件
    sencha touch笔记(6)——路由控制(1)
    [置顶] Android源码分析-点击事件派发机制
    UVa 10330 Power Transmission / 最大流
  • 原文地址:https://www.cnblogs.com/Innovate/p/5465196.html
Copyright © 2011-2022 走看看