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 7992833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:40:57+00:00 2026-06-04T13:40:57+00:00

I have 2 PartialViews inside a parent View. In these 2 PartiaViews I generate

  • 0

I have 2 PartialViews inside a parent View.

In these 2 PartiaViews I generate some JavaScript code which needs to be included to the bottom of the parent View.

Here is the HtmlHelper extension which generates some JavaScript in the PartialViews.

public static MvcHtmlString KO_ObjectFor<TModel>(this HtmlHelper<TModel> htmlHelper)
{
    String str = "some javascript code";
    return MvcHtmlString.Create(str);
}

This is the parent View (main View)

...
//First Partial View
<% Html.RenderPartial("_PartialViewFirst"); %>

//Second Partial View
<% Html.RenderPartial("_PartialViewSecond"); %>

//Here I need to display the JavaScript from the PartialViews
<script type="text/javascript">
   <%: Html.FunctionToConcatenateJavaScript() %>
</script>
...

What would be the right approach?

  • 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-04T13:40:58+00:00Added an answer on June 4, 2026 at 1:40 pm

    You could use a Stack to push and pop scripts in the HttpContext:

    public static class HtmlExtensions
    {
        private const string ScriptsKey = "__scripts__";
    
        public static MvcHtmlString KO_ObjectFor<TModel>(this HtmlHelper<TModel> htmlHelper)
        {
            var stack = htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] as Stack<string>;
            if (stack == null)
            {
                stack = new Stack<string>();
                htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] = stack;
            }
            String str = "some javascript code";
            stack.Push(str);
    
            return new HtmlString("some code that the helper needs to generate and output to the view");
        }
    
        public static IHtmlString FunctionToConcatenateJavaScript(this HtmlHelper htmlHelper)
        {
            var stack = htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] as Stack<string>;
            if (stack == null)
            {
                return MvcHtmlString.Empty;
            }
    
            var scriptTag = new TagBuilder("script");
            scriptTag.Attributes["type"] = "text/javascript";
            var sb = new StringBuilder();
            foreach (var script in stack)
            {
                sb.AppendLine(script);
            }
            scriptTag.InnerHtml = sb.ToString();
    
            return new HtmlString(scriptTag.ToString());
        }
    }
    

    then you could push:

    <%= Html.KO_ObjectFor(x => x.Foo) %>
    <%= Html.KO_ObjectFor(x => x.Bar) %>
    ...
    

    and then pop em all in the master:

    <%= Html.FunctionToConcatenateJavaScript() %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one form tag inside my Index.aspx view. Index.aspx contains several partial
i have the following Ajax.actionlink inside a view to add an answer under a
Scenario: I have a View and inside that view I also a partial view.
I have a partial view that upon adding an item to the database needs
I have a Partial View with two opposite ActionLinks inside: <div id=divStatus> @if (IsAuthenticated)
Inside of an asp.net mvc partial view, I have an Ajax form that posts
I have a view that is strongly typed. Inside this view i have jqueryui
I have a partial view which consists of a div with a benner that
I have created VIEWS and PartialVIEWS, but so far, i have seen that VIEWS,
I have a partialview [_SearchProduct] within the main view, let's say [product] view. The

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.