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

  • SEARCH
  • Home
  • 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 8046621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:50:44+00:00 2026-06-05T05:50:44+00:00

I need to have all my scripts at the bottom of the page, problem

  • 0

I need to have all my scripts at the bottom of the page, problem is when I have a Partial View I cannot use the “RenderSection” approach. Found a great example of how to add a HtmlHelper extension which takes the name of a script file, loads into a stack, then another helper renders that out on the base layout:
Razor section inclusions from partial view

That’s great – but I don’t want to have to create an entire JS file for a little chunk of script, or maybe even HTML, that I want to drop in. And I don’t want to pass it all as a string, I want the nice formatting and intellisense, so I want to use a template ie:

@{Html.AddScript("test", @<text>
<script type="text/javascript">
    function RefreshPreview() {
        $('#AutoReplyHtml_Preview').html(
            $('#htmlTemplate').html()
                .replace('@@MESSAGE_TITLE@@', $('#AutoReplySubject').val())
                .replace('@@PRE_HEADER@@', $('#AutoReplyPreHeader').val())
                .replace('@@MESSAGE_BODY@@', $('#AutoReplyHtml').val())
        );

        $('#AutoReplyPlainText_Preview').html(
            $('#plainTextTemplate').html()
                .replace('@@MESSAGE_BODY@@', $('#AutoReplyPlainText').val())
        );
    }

    $(document).ready(function() {
        RefreshPreview();
    });
</script>
</text>);}

Problem is – how to I get the value of the template into my method, I have this code which complies, but no clue how to get the data out of the “code” parameter:

    public static string AddScript(this HtmlHelper htmlHelper, string title, Func<object, object> code) {
    var ctx = htmlHelper.ViewContext.HttpContext;
    Dictionary<string, string> scripts = ctx.Items["HtmlHelper.AddScript"] as Dictionary<string, string>;
    if (scripts == null) {
        scripts = new Dictionary<string, string>();
        ctx.Items.Add("HtmlHelper.AddScript", scripts);
    }

    scripts.Add(title, code.ToString()); //Doens't work!

    return string.Empty;
}

How do I need to tweak the delegate parameter to get the value inside the template??

  • 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-06-05T05:50:45+00:00Added an answer on June 5, 2026 at 5:50 am

    The helper architecture is designed so that it can accomodate scenarios where you are providing a template that will operate, for example, on each item in a list. In such a scenario, you’d of course want to be able to pass it the “current” item when iterating through the list.

    However, in other scenarios (such as yours), there is no current item. Yet, as you’ve discovered, you still have to declare a delegate as a parameter to your method that defines a method that consumes one parameter. That’s ok — since you’re not consuming that argument in your helper (you don’t make use of the somewhat magical item parameter in your template) you can just pass it null in your implementation. Preferably, declare your parameter as a Func<object, IHtmlString> rather than a Func<object, object>, but regardless, just invoke code(null).ToString() to get the HTML-encoded string you need to render.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the scripts I have found via Google search rely on some sort of
On large-scale Java/.Net Enterprise projects, does every developer need to have all the components/libraries/dependencies
I have a whole bunch of buttons that all need to have both a
The consensus seems to be that all foreign keys need to have indexes. How
I need to parse a bunch of incoming xml documents, they all have the
I need my application run in multiple screen resolutions. Currently I have specified all
I need all records which have year entered from search criteria. for ex: String
I have multiple threads who all need to write to the same Dictionary. I
I need get all items these have no categories int? categoryId = null; var
I have one scenario where I need to select all files having aliencoders.numeric-digits like

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.