1

In a partial view I load a javascript file like this :

<script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>

In the Testing.js, there are some functons defined That's work, the functions defined in the file can be used.

Now I do an action in my application, my controller return an another (where I don't load any js file) partial view. The functions defined in Testing.js are still available in this partial view.

Is there a way to "remove" the functions loaded in the first view ?

Thanks,

Update 1

I tried this in the partial view but error : The file "~/Views/xxxx.cshtml" cannot be requested directly because it calls the "RenderSection" method.

@section MyScript {
    <script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>
}
@RenderSection("MyScript", false)
5
  • 2
    Why are you loading javascript in a partial in the first place? Javascript should be loaded only once in your Layout. There must be something wrong with your design. What are you trying to achieve (and don't answer me please: I want to remove functions loaded in the first view)? Commented Jul 26, 2012 at 7:41
  • 1
    I've faced the same problem. The solution is what @DarinDimitrov has suggested. Another problem with your approach is that if you load the partial view again, you'll have multiple scripts (same) in your page. Commented Jul 26, 2012 at 7:43
  • @DarinDimitrov in the first view the JS loaded is specific for this partial view and not needed in other. Ok, I can load in the layout. But the script can be huge at the end. And I don't see when I look the source code via the browser the JS function called in the partial view, for security reason is not better ? Commented Jul 26, 2012 at 7:46
  • When I meant put it in the Layout, I didn't mean literary stuffing your entire script inline there. I meant that you should use an external javascript file that you should reference. And you should reference it in a scripts section from the view so that this script is included only for views that need it, not all of them. As far as the security reasons that you are pointing out, in both situations there strictly 0 security. In both cases javascript is completely accessible to the client. So don't think about any security if you are doing javascript as there's none. Commented Jul 26, 2012 at 7:48
  • The RenderSection call should be done in your Layout, not in your view. What's the point of defining a section in the view and rendering it immediately after in this view? Commented Jul 26, 2012 at 9:03

1 Answer 1

6

You should avoid referencing any scripts in partials. You could define a section in your Layout, for example just before the closing </body> which will allow for views to include some custom scripts:

    <script type="text/javascript" src="@Url.Content("~/scripts/some_common_script_that_will_be_used_by_all_views_such_as_jquery_for_example")"></script>
    @RenderSection("scripts", false)
</body>
</html>

and then in the view (not in the partial view) override this section to include any scripts that this view might need:

@section scripts {
    <script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>
}
Sign up to request clarification or add additional context in comments.

7 Comments

And if I have only some index pages and a lot of partial ?
You include the script in every view (not partial) that is susceptible to include those partials. You could have a gazillion of partials included in a main view. It doesn't matter. Remember that the final product of your application is a single HTML page. That's what the browser sees. The browser doesn't know what a partial means. So stop thinking about partials. Think in terms of functionality that your application offers and include the script in each page that will require it.
Ok than, in my case, I need to load in the layout because the main screen has a menu on the left and on the right is a <div> where I load partial view.
You have 2 possibilities: either you use Ajax.* helpers and include the unobtrusive ajax script or you use standard helpers and write javascript to AJAXify them that you include in your views.
@Shimmy, it is by design. Scripts don't belong to partial views. Simply register those scripts in the parent view hosting those partials.
|

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.