26

I got a view with Layout defined. In the layout, there is a section:

@RenderSection("JavaScript", required: false)

In the view, based on a condition, I want to either render certain JavaScript to the section or not.

@if (condition)
{ 
    @section JavaScript
    {
        <script type="text/javascript>
           $(document).ready(function(){
             //bla...
           });
        </script>
    }
}

The above syntax is wrong. What's the correct syntax?

Edit

Basically, I got a partial view which needs to be rendered based on a condition. If the condition is true, then I need to render the partial view together with some JavaScript for the partial view, which I want to go to the "JavaScript" section. How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Dimskiy
  • 5,233
  • 13
  • 47
  • 66

2 Answers2

50

@section blocks can only appear in markup contexts.

You can write

@if (condition)
{ 
    <text>
        @section JavaScript
        {
            <script type="text/javascript>
               $(document).ready(function(){
                 //bla...
               });
            </script>
        }
    </text>
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • You can also use a comment to change the markup context, so that you don't need a spurious tag, ie. @if (condition) { \@section JavaScript – Deeksy Sep 22 '14 at 00:22
  • @Deeksy: `` is a Razor tag; it is not emitted in the output. – SLaks Sep 22 '14 at 02:22
2

@if(condition) RenderSection(..)

@Section Javascript{ 
        <script type="text/javascript>
           $(document).ready(function(){
             //bla...
           });
        </script>
}

or in your layout:

@RenderSection(..)

and in your view:

@section Javascript{ 
if(condition) 
{
    <script type="text/javascript">
        $(document).ready(function () {
            //bla...
        });
    </script>
} 
} 

also see: Is there a way to make a @section optional with the asp.net mvc Razor ViewEngine?

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Is this inside of the Layout? The layout doesn't know about the condition. – Dimskiy May 12 '11 at 18:40
  • then your condition is inside of your section as opposed to the other way around that you have up top. – Adam Tuliper May 12 '11 at 18:45
  • Ok. Essentially, I have a condition inside the view. If it's true, I need to render a partial view together with some JavaScript for the partial view. So, on the view level, how can I specify to either render the section or not? – Dimskiy May 12 '11 at 18:50
  • In the view simply have @if(condition){RenderSection(....)} or are you referring to something else? – Adam Tuliper May 12 '11 at 19:02
  • RenderSection(....) is a line of code inside of Layout. @section SectionName{....} is code inside of view. Putting RenderSection() in view results in The file "~/Views/MyView/Bla.cshtml" cannot be requested directly because it calls the "RenderSection" method. Your example has a condition inside of Layout. I need to have that condition inside of View. Or am I not getting something? – Dimskiy May 12 '11 at 19:08
  • ah I see your structure. Your condition must either be passed to the layout to use (viewbag or see http://stackoverflow.com/questions/5973208/net-mvc3-getting-a-reference-to-the-page-from-a-controller/5973573#comment-6902117 ) or your bla.cshtml must have that condition inside of the @section declaration . I'll edit answer above to show – Adam Tuliper May 12 '11 at 20:14