1

If I use @section scripts in partial view. page is not rendering script. But If remove @section scripts js rendering to the page. What is the difference? Can't I use directly <Script> in partialviews? not good practice?

 @section scripts
{
    <script type="text/javascript">
        $("#SideMenu > li").each(function () {
            //Find the inner element with active class
            $(this).find('.active').removeClass('active');
        });
    </script>
}
3
  • 1
    not a good practice. Better to put your code in a function on the main view and call the function after the partial is loaded Commented Apr 30, 2015 at 18:25
  • But I have sidemenu is partialview which is in _Layout file. If I use @section in _layout Partialview will not invoke. This is common menu for all views. So I kept in _layouts. Now how to manage it. Commented Apr 30, 2015 at 18:57
  • 1
    then you should put your script in a separate file and make sure that it is also included in the layout page. You should still not have script on your partial view Commented Apr 30, 2015 at 18:59

2 Answers 2

1

If your "scripts" section is defined in your _Layout page, then it would not generate this section since partial views do not invoke the _Layout view.

Typically you'd want to include all the necessary jquery code in your main view, as this will affect any partial views that are initially created with the view. Now if you have a partial view that is called after view creation, you can simply add your tags to the bottom of the partial view.

Sign up to request clarification or add additional context in comments.

2 Comments

But I have sidemenu is partialview which is in _Layout file. If I use @section in _layout Partialview will not invoke. This is common menu for all views. So I kept in _layouts. Now how to manage it
I would forget about using @section in your partials and simply add your <script> code to the bottom of the partial view instead.
0

Section in partial view doesn't work by design. See this answer for detail: https://stackoverflow.com/a/7556594/1817929

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.