Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Handling in MVC3

So, continuing in climbing my MVC learning curve, I would like to know how to effectivelly handle javascript in partial views. I mean coding a script in a partial view and then rendering the partial view twice in a view gives duplicate code, including variables and is generally conflictive.

How pros handle JS concurrency in ASP.NET MVC so each partial view see only its own JS code?

like image 585
Luis Aguilar Avatar asked Apr 01 '11 17:04

Luis Aguilar


1 Answers

When you render a partial view within a view, you can still reference all of the HTML elements in that partial view from Javascript on the holding view.

This will not only help with duplication, but will add to the principle that code should be maintainable within one place in the codebase.

If I were you, I would put all Javascript that your partial views need in the main view, or better yet, in a Javascript file that is separate and reference it in a master page which your main view inherits from :)

like image 110
slandau Avatar answered Oct 20 '22 03:10

slandau