27

As the title states, I want to define a section in a partial view.

My code that I've tested with are as follows:

Controller:

public ActionResult Test()
{
    return View();
}

public ActionResult PartialTest()
{
    return PartialView("_PartialTest");
}

Test.cshtml:

@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>

@Html.Action("PartialTest")

_PartialTest.cshtml:

<p>partial Test</p>

@section scripts {
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Test");
         });
     </script>
}

Placing the section scripts in the Test.cshtml works fine so the problem isn't in the layout.

Anyone know how to do this?

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Jeandre Pentz
  • 992
  • 2
  • 9
  • 20
  • This seems to have done the trick for me: http://stackoverflow.com/questions/7556400/injecting-content-into-specific-sections-from-a-partial-view-asp-net-mvc-3-with#15971504 – Schalk Aug 12 '14 at 06:24
  • Possible duplicate of [Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine](https://stackoverflow.com/questions/7556400/injecting-content-into-specific-sections-from-a-partial-view-asp-net-mvc-3-with) – Matyas Feb 12 '19 at 06:26

2 Answers2

41

Partial views don't support @section tag. You should add them in the view which references the partial view. See this question for more information: Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine.

It basically comes down to the fact that the main view referencing a partial should be responsible for including Javascript, not the partial view itself.

Community
  • 1
  • 1
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • 31
    Although this is the answer to the question (that it is not supported), it begs the question, why are partial views so limited? In some applications, you would like the ability to create "widgets" and what other way to create a "widget" than to use a partial view? Except for the fact that it is so very limited and is heavily dependent on it's parent view which totally defeats the purpose. Is there an equivalent to a user control (web forms) in MVC razor? – clockwiseq Aug 21 '15 at 05:33
  • 2
    I know this is an old question but I'm trying to find the solution to exactly this: wanting to include 'widget' specific script/jquery in the view in which it is rendered? Anyone got a good solution? – Kevin Dark Aug 28 '16 at 22:02
  • It would be pretty easy to write some sort of script manager / extension that you could call from any module and then have a method to print out unique/distinct sets of script tags on your parent/master view. – Kris Oye Aug 31 '16 at 06:48
  • is this the same for later versions? It would be nice to support sections in a partial to organize the scripts associated to only that partial. I guess abstracting them out to a js file is another solution. – eaglei22 Oct 09 '18 at 17:48
6

I know this question is super out-dated, but for anyone out there who might still wondering (as I was):

You can actually get around this problem by changing the @section scripts { } portion in your partial views to this:

@using (Html.BeginScriptContext())
{
    Html.AddScriptBlock(
        @<script type="text/javascript">
        //rest of script
    );
}

This will allow you to have a script in your partial view and achieve that widget-like structure. Hope this helps!

keenns
  • 863
  • 4
  • 14
  • 26
  • 1
    I think this is referring to functionality provided by a third-party package ForLoop.HtmlHelpers (https://www.nuget.org/packages/Forloop.HtmlHelpers/) – Chris Haines Jul 04 '17 at 12:00
  • @ChrisHaines - In this package there is an option to include the script file in the partial view using the Html.AddScriptFile("path of script file") extension method, but do you know how to add script bundle inside the partial view using same script context – Ashish Shukla Sep 06 '18 at 10:48