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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:11:30+00:00 2026-05-20T02:11:30+00:00

In my MVC 2 site, I have an html helper, that I use to

  • 0

In my MVC 2 site, I have an html helper, that I use to add javascripts for my pages. In my master page I have the main javascripts I want to include, and then in the aspx pages, I include page specific javascripts.

So for example, my Site.Master has something like this:

....
<head>
    <%=html.renderScripts() %> 
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....

Then in the child aspx page, I may also want to include other scripts.

...
//the page specific script I want to use
<% html.AddScript("/scripts/register.aspx.js") %>
...

So when the full page gets rendered the javascript files are all collected and rendered in the head by sitemaster placeholder function RenderScripts. This works fine.

Now with MVC 3 and razor view engine, they layout pages behave differently, because now my page level javascripts are not rendered/included. Now all I see the LayoutMaster contents.

How do I get the solution wo workwith MVC 3 and the razor view engine. (The helper has already been re-written to return a HTMLString ;-))

For reference: my MasterLayout looks like this:

  ...
  ...
  <head>
  @{
      Html.AddJavaScript("/Scripts/jQuery.js");
      Html.AddJavaScript("/Scripts/myLib.js");
   }
   //Render scripts
   @html.RenderScripts()
  </head>
 ....

and the child page looks like this:

@{
    Layout = "~/Views/Shared/MasterLayout.cshtml";
    ViewBag.Title = "Child Page";
    Html.AddJavaScript("/Scripts/register.aspx.js");
 }
 ....
 <div>some html </div>

Thanks for your help.

Edit = Just to explain, if this question is not clear enough.

When producing a “page” I collect all the javascript files the designers want to use, by using the html.addJavascript(“filename.js”) and store these in a dictionary – (1) stops people adding duplicate js files – then finally when the page is ready to render, I write out all the javascript files neatly in the header. (2) – this helper helps keep JS in one place, and prevents designers from adding javascript files all over the place. This used to work fine with Master/SiteMaster Pages in mvc 2. but how can I achieve this with razor?

Edit: html helper code:

....
private const string SCRIPTLINK = 
        "<script src=\"{0}\" type=\"text/javascript\"></script>";

public static HtmlString RenderScripts(this HtmlHelper helper)  {

    OrderedDictionary scripts = 
        helper.ViewContext.HttpContext.Items["JavaScripts"] as OrderedDictionary;

    StringBuilder sb = new StringBuilder(500);
    foreach (DictionaryEntry script in scripts)  {
                sb.AppendFormat(SCRIPTLINK, script.Key);
        }
     return new HtmlString(sb.ToString());
 }
....
  • 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-20T02:11:31+00:00Added an answer on May 20, 2026 at 2:11 am

    Thanks for the help and tips Slaks and David. I finally got a solution that seems to work, but with some things I don’t like. If there is a better way, please feel free to point it out.

    Here is my Child Page:

    @{
        ViewBag.Title = "Child Page";
     }
    
     @section Scripts {
    
         @{Html.AddJavaScript("/Scripts/register.aspx.js");}
    
      }
    
     <div>some html </div>
    

    I then have a _ViewStart.cshtml with:

    @{
        this.Context.Items.Add("JavaScripts", 
            new System.Collections.Specialized.OrderedDictionary());
    
        Layout = "~/Views/Shared/MasterLayout.cshtml";
    }
    

    And my MasterLayout looks like this:

    <html>
       ....
       <title>@ViewBag.Title</title>
       <head>
             @{
                //core scripts
                html.AddScript("/scripts/jquery.js"); 
                html.AddScript("/scripts/myLib.js");
                ....
               }
    
               @RenderSection("Scripts", required:true) 
    
               //Have to call render scripts here, but not sure about this?
               @Html.RenderScripts()
       </head>
    ....
    </html>
    

    This does render my script tags in the correct order and place, but does require the client to call the html helper renderScripts in child pages. which is ok, as we can create templates to help with that. Any better ideas?

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

Sidebar

Related Questions

I have an ASP.NET MVC site (Master Page + Content pages). On the master
I have a small MVC site that uses the Html.ActionLink helper for a navbar.
I add that to my header <%@ Page Language=C# MasterPageFile=~/Views/Shared/Site.Master Inherits=System.Web.Mvc.ViewPage %> and am
I have a search page on my MVC site that contains a list of
I have a asp.net mvc site with a master page and it has following
I have a simple view: <%@ Page Title= Language=C# MasterPageFile=~/Views/Shared/Site.Master Inherits=System.Web.Mvc.ViewPage %> <asp:Content ID=Content1
I have an ASP.NET MVC site and ELMAH is showing me that my site
I have a asp.net mvc site that references a couple of libraries. Recently I
I have a very simple MVC site that is returning a 404 not found
I am creating a very large PHP MVC-based site that will have a large

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.