Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:24:14+00:00 2026-05-31T05:24:14+00:00

I have section for scripts in my _Layout.cshtml: <html> <body> … @RenderSection(FooterScript, required: false)

  • 0

I have section for scripts in my _Layout.cshtml:

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

I have view “Index.cshtml” that contains @Html.RenderPartial("LinksBlock", someModel).
LinksBlock partial requires script file “links.js”. I want to make links.js inclusion into FooterScript from my partial view, not from main view (main view don’t know about dependencies of partial view), and I want to be sure, that if I use more than 1 LinksBlock in my view, only 1 links.js was included. Is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:24:14+00:00Added an answer on May 31, 2026 at 5:24 am

    Sections do not work with partial views. But you could write a pair of custom helpers that could be used in conjunction:

    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 your partial:

    @{Html.RegisterScript("~/scripts/foo.js");}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a Layout page <head> @RenderSection(HeaderLast, required: false) </head> A view @section
In _Layout.cshtml I have the following defined in the tag: @RenderSection(JavaScript, required: false); The
I have scripts in my ASPX page, in the header section. I want to
I have a bash script that cuts out a section of a logfile between
I have a section of makefile that has this sort of structure: bob: ifdef
I have a section of code that is being used to determine if a
I have a section of code that can be summarised as follows; void MyFunc()
I have a main script that is calling on several others scripts and I
@ /index.php/admin/system_config/edit/section/design/ Does adding to Magento's Miscellaneous scripts field affect every page in the
I have some user controls in ASP.NET that I am using as simply HTML,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.