Populate a Razor Section From a Partial

前端 未结 12 1495
不知归路
不知归路 2020-11-27 11:23

My main motivation for trying to do this is to get Javascript that is only required by a partial at the bottom of the page with the rest of the Javascript and not in the mid

12条回答
  •  渐次进展
    2020-11-27 11:44

    Base on the answer from Mr Bell And Shimmy above, I add on extra function for Bundle script.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.Web.Mvc;
    namespace ABC.Utility
    {
    public static  class PartialViewHelper
    {
        public static string RequireScript(this HtmlHelper html, string path, int priority = 1)
        {
            var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List;
            if (requiredScripts == null) HttpContext.Current.Items["RequiredScripts"] = requiredScripts = new List();
            if (!requiredScripts.Any(i => i.Path == path)) requiredScripts.Add(new ResourceInclude() { Path = path, Priority = priority });
            return null;
        }
    
        public static string RequireBundleStyles(this HtmlHelper html, string bundleName)
        {
            var a = System.Web.Optimization.Styles.Render(bundleName);
            var requiredStyles = HttpContext.Current.Items["RequiredStyles"] as IHtmlString;
            if (requiredStyles == null) HttpContext.Current.Items["RequiredStyles"] = requiredStyles = a;
            return null;
        }
    
        public static string RequireBundleScripts(this HtmlHelper html, string bundleName)
        {
            var a=System.Web.Optimization.Scripts.Render(bundleName);
            var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as IHtmlString;
            if (requiredScripts == null) HttpContext.Current.Items["RequiredScripts"] = requiredScripts = a;
            return null;
        }
    
        public static HtmlString EmitRequiredBundleStyles(this HtmlHelper html)
        {
            var requiredStyles = HttpContext.Current.Items["RequiredStyles"] as IHtmlString;
            if (requiredStyles == null) return null;
            return MvcHtmlString.Create(requiredStyles.ToHtmlString()) ;
        }
    
        public static HtmlString EmitRequiredBundleScripts(this HtmlHelper html)
        {
            var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as IHtmlString;
            if (requiredScripts == null) return null;
            return MvcHtmlString.Create(requiredScripts.ToHtmlString());
        }
    
        public static HtmlString EmitRequiredScripts(this HtmlHelper html)
        {
            var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List;
            if (requiredScripts == null) return null;
            StringBuilder sb = new StringBuilder();
            foreach (var item in requiredScripts.OrderByDescending(i => i.Priority))
            {
                sb.AppendFormat("\n", item.Path);
            }
            return new HtmlString(sb.ToString());
        }
        public class ResourceInclude
        {
            public string Path { get; set; }
            public int Priority { get; set; }
        }
    }//end class
    }// end namespace  
    

    Sample on PartialView :- @Html.RequireBundleStyles("~/bundles/fileupload/bootstrap/BasicPlusUI/css"); @Html.RequireBundleScripts("~/bundles/fileupload/bootstrap/BasicPlusUI/js");

    Sample on MasterPage :- @Html.EmitRequiredBundleStyles()

提交回复
热议问题