很多情况Partial是需要引用到JS的,通常做法是吧JS在引用Partial的页面中加入JS文件或者JS代码。前阵子网上看到一段代码可以在Partial页面中添加JS,输出道引用页面。 public static class HtmlExtensions { priva ...
很多情况Partial是需要引用到JS的,通常做法是吧JS在引用Partial的页面中加入JS文件或者JS代码。
前阵子网上看到一段代码可以在Partial页面中添加JS,输出道引用页面。
public static class HtmlExtensions { private const get='_blank'>string JscriptDeferRazorViewdata = "__jsdfrz"; private const string JscriptIncludeViewdata = "__jsrq"; public static void DeferScript(this HtmlHelper html, string scriptLocation) { string jsTag = "<script type=\"text/javascript\" src='/images/loading.gif' data-original=\"" + 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); } }); } }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:ASP.NET MVC Partial页输出JS
关键词:ASP.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。