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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:57:30+00:00 2026-06-15T22:57:30+00:00

I have an ASP.NET MVC3 application in C# and Razor. I use the standard

  • 0

I have an ASP.NET MVC3 application in C# and Razor.

I use the standard _Layout.cshtml from the MVC3 application template in VS2010. I would like to add Javascript code in one of my Partial View just when the View is rendered and not attach the Javascript directly in the _Layout.

My View looks like this:

@model MyNameSpace.ViewModels.MyViewModel

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Filters</legend>

        @{ Html.RenderPartial("Filters", Model.Filters);  }

        <p>
            <input type="submit" value="Submit" />
        </p>

    </fieldset>
}

And my Partial View Filters.cshtml looks like this:

@model MyNameSpace.ViewModels.FiltersViewModel

<p>
@Html.DropDownListFor(
    x => x.Type, 
    new SelectList(Model.Types, "Value", "Text"),
    "-- select type --"
)
</p>

<p>
@Html.DropDownListFor(
    x => x.Category, 
    new SelectList(Model.Categories, "Value", "Text"),
    "-- select category --"
)</p>

<script type="text/javascript">
   //Some Javascript
</script>

If I leave the View like this, the Javascript is rendered inside the <body> and it looks very ugly. I can add the Javascript directly in the <head> section of the _Layout but it is not what I want. How can I achieve my purpose?

  • 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-15T22:57:31+00:00Added an answer on June 15, 2026 at 10:57 pm

    add @RenderSection("HeadContent", required: false) inside your main layout (masterpage) and then add something like

        @section HeadContent {
            <link href="@Url.Content("~/Content/styles/admin.css")" rel="stylesheet" type="text/css" />
            <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>    
        } 
    

    in your partial view

    When razor will compiling html page it will place content from @section block from the partial view to the place where you put @RenderSection(“HeadContent”, required: false) on the layout (masterpage). If there are no @section block on the partial view, then nothing will be added.

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

Sidebar

Related Questions

I have a Razor view in my asp.net MVC3 application with a dropdownlist like
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application
I have built an ASP.NET MVC 3 web application (with exlusively Razor/cshtml pages) that
I have an ASP.NET MVC3 application in C# and Razor. In the view MyView
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application
I have a application on asp.net mvc3 and working in a razor view. On
Here's the issue at hand: I have developed an ASP.NET MVC3 application using Razor.
I have an ASP.NET MVC3 web application with C# and Razor. Through a form
I have standard ASP.NET 4 application. Inside this application there are also few razor
i have made an application in asp.net mvc3/c#/Razor , now i want to implement

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.