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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:25:12+00:00 2026-05-26T05:25:12+00:00

What is the best way of adding external files from a partial view? I

  • 0

What is the best way of adding external files from a partial view?

I need to do something like this

code in partial view:

@{
    var files = new List<string>();
    files.Add("some path");
    files.Add("some path");
    ViewBag.Files = files;
}

in layout page

@foreach (var file in ViewBag.Files) {
    @file
}

this does not actually work though

  • 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-26T05:25:13+00:00Added an answer on May 26, 2026 at 5:25 am

    As promised

    You can’t render @section‘s as it’s not supported when rendered by Partial Views, until then you can do this trick:

    in your _Layout.cshtml write

    @RenderSection("scripts", false)
    @Html.RenderSection("scripts")
    

    The first one is the default, the second line is your brand new way to render a section, I use both in my code…

    Now let’s add some code to our Partial View

    instead of

    @section scripts { 
       <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
       <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    }
    

    replace it with:

    @Html.Section(
        @<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>, "scripts"
    )
    @Html.Section(
        @<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>, "scripts"
    )
    

    and our little helper that you just put inside your Models folder and reference it in your Partial View and Layout Page

    // using idea from http://stackoverflow.com/questions/5433531/using-sections-in-editor-display-templates/5433722#5433722
    public static class HtmlExtensions
    {
        public static MvcHtmlString Section(this HtmlHelper htmlHelper, Func<object, HelperResult> template, string addToSection)
       {
          htmlHelper.ViewContext.HttpContext.Items[String.Concat("_", addToSection, "_", Guid.NewGuid())] = template;
          return MvcHtmlString.Empty;
       }
    
       public static IHtmlString RenderSection(this HtmlHelper htmlHelper, string sectionName)
       {
          foreach (object key in htmlHelper.ViewContext.HttpContext.Items.Keys)
          {
             if (key.ToString().StartsWith(String.Concat("_", sectionName, "_")))
             {
                var template = htmlHelper.ViewContext.HttpContext.Items[key] as Func<object, HelperResult>;
                if (template != null)
                {
                   htmlHelper.ViewContext.Writer.Write(template(null));
                }
             }
          }
          return MvcHtmlString.Empty;
       }
    }
    

    To render CSS, all you need to so is using other section name, for example:

    In _Layout.cshtml

    @Html.RenderSection("styles")
    

    in your Partial Views

    @Html.Section(
      @<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">, "styles"
    )
    

    I hope this helps.

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

Sidebar

Related Questions

What is the best way of adding a new object in the entity framework.
Im planning on adding a instructions view to my app. Whats the best way
What is the best way to make something like a mask with rounded corners
What's the best way of adding spaces between strings myString = string.Concat(a, ,b) or
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
I'm looking for the best way to conditionally avoid adding an element to a
What is the best way to integrate an external script into the Zend Framework?
I am having problems adding GMarkers when using a loop. The best way to

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.