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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:29:24+00:00 2026-05-28T18:29:24+00:00

In a .NET MVC partial view, I’d like to do something like this: <%

  • 0

In a .NET MVC partial view, I’d like to do something like this:

<% ScriptCollection.RequireScript("path/to/script.js"); %>

That would require a method in the ScriptCollection object that looks like this:

public void RequireScript(string src)
{
    if (!_List.Contains(src))
        _List.Add(src);
}

Then, in my Master page, I would have something like this:

<html>
<head></head>
<body>

    <!-- Content would go here -->

    <% foreach (var script in ScriptCollection) { %>
        <script type="text/javascript" src="<%= script %>"></script>
    <% } %>

</body>
</html>

My question is this:
How can I make the ScriptCollection object available to be updated by a partial view and also available to be used by a Master page?

Edit:
I do not want to add the required scripts in a controller, nor do I want to use a strongly typed Master page. Though suggestions on doing so are welcome if those methods are considered a best practice.

Edit #2:
This would be easy with extension properties. I could just give the HtmlHelper class the ScriptCollection property. Am I missing something like this that already exists?

  • 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-28T18:29:25+00:00Added an answer on May 28, 2026 at 6:29 pm

    You could take a look at Telerik’s ScriptRegisterBuilder class implementation which does exactly what you want. Or you can just use it as well. In addition it also has support for:

    1. Combining multiple JS files into one which result in less requests by the browser.
    2. Compressing JS files.
    3. HTTP Caching.
    4. Content Delivery Networks.
    5. release/debug versions of your JS resources depending on your build mode.

    Now even when you don’t use any of the other stuff Telerik provides you can use the ScriptRegisterBuilder class but then it’s advised to disable the automatic jquery & jquery validation script registration. See also the link provided.

    Also take a look the StyleSheetRegistrarBuilder class. This is the nephew of the ScriptRegisterBuilder class and handles the CSS assets.

    I did noticed the MVC2 tag in your question but my simple example uses razor and is just for making the point. It shouldn’t be hard to make it work for MVC2/ASPX as well. Otherwise let me know.

    Master

    @using Telerik.Web.Mvc.UI
    <!DOCTYPE html>
    <html>
    <head>
        <title>@ViewBag.Title</title>  
        @Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site.css"))  
    </head>
    <body>
        @RenderBody()
    
        @Html.Telerik().ScriptRegistrar().jQuery(false).jQueryValidation(false)    
    </body>
    </html>
    

    Partial View

     @using Telerik.Web.Mvc.UI
     @{
       ViewBag.Title = "Home Page";
       Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site2.css"));
       Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Add("~/Scripts/Test.js"));
      }
    
      <h2>@ViewBag.Title</h2>
      <p>
         Bla bla bla
      </p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.Net MVC I would like to render a different partial view depending on
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
ASP .NET MVC 1 I'd like to show a partial view base on a
I have an Asp.net MVC partial view that is used for searching. It does
I would like to make a database call in a partial view in asp.net
Inside of an asp.net mvc partial view, I have an Ajax form that posts
asp.net mvc 3 with razor I have a script inside a partial view <script
Would someone confirm the best place for a partial view in ASP.NET MVC? My
I'm currently trying to add an MSChart to a partial view in ASP.NET MVC
I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial

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.