Turn on razor sections with partial view

I have a section for scripts in my _Layout.cshtml:

<html> <body> ... @RenderSection("FooterScript", required: false) </body> </html> 

I have a view "Index.cshtml" that contains @Html.RenderPartial("LinksBlock", someModel) . The LinksBlock element requires the script file "links.js". I want to include link.js in FooterScript from my partial view, and not from the main view (the main view does not know about the dependencies of the partial view), and I want to be sure that if I use more than 1 LinksBlock in my view, only 1 link. js was included. Is it possible?

+2
source share
1 answer

Sections do not work with partial views. But you could write a couple of custom helpers that could be shared:

 public static class HtmlExtensions { public static IHtmlString RegisteredScripts(this HtmlHelper htmlHelper) { var ctx = htmlHelper.ViewContext.HttpContext; var registeredScripts = ctx.Items["_scripts_"] as Stack<string>; if (registeredScripts == null || registeredScripts.Count < 1) { return null; } var sb = new StringBuilder(); foreach (var script in registeredScripts) { var scriptBuilder = new TagBuilder("script"); scriptBuilder.Attributes["type"] = "text/javascript"; scriptBuilder.Attributes["src"] = script; sb.AppendLine(scriptBuilder.ToString(TagRenderMode.Normal)); } return new HtmlString(sb.ToString()); } public static void RegisterScript(this HtmlHelper htmlHelper, string script) { var ctx = htmlHelper.ViewContext.HttpContext; var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); var registeredScripts = ctx.Items["_scripts_"] as Stack<string>; if (registeredScripts == null) { registeredScripts = new Stack<string>(); ctx.Items["_scripts_"] = registeredScripts; } var src = urlHelper.Content(script); if (!registeredScripts.Contains(src)) { registeredScripts.Push(src); } } } 

And then in your _Layout.cshtml :

 @Html.RegisteredScripts() 

and in partial:

 @{Html.RegisterScript("~/scripts/foo.js");} 
+14
source

All Articles