I need to write the contents of a js file from a convention-driven location (like ~/ClientApp/Controllers/Home/Home.js if loading the view located at ~/Views/Home/Home.cshtml). How do I do this?
Example: if the file ~/Views/Home/Home.cshtml looks like:
<div id="some-partial-view">
<!-- ... -->
</div>
And the file ~/ClientApp/Controllers/Home/Home.Controller.js looks like
function HomeController() {
//some code
}
Then the rendered view returned by the webserver should look like (if using fiddler)
<!--ommitted <html> <body> tags -->
<div id="some-partial-view">
<!-- ... -->
</div>
<script type="text/javascript">
function HomeController() {
//some code
}
</script>
One way is to add an HTML Helper that will do this such as:
<div id="some-partial-view" ng:Controller="HomeController">
<!-- ... -->
</div>
@Html.IncludeController("HomeController")
However, i don't want to repeat this in all partial views.
Any ideas?
it would make more sense to include all you javascript in the outter ring, then include that via index.php or wherever...
Personally I use jQuery and I include each object like so...
You could write a custom view:
and a custom view engine which will return this custom view when a partial view is to be requested:
which would be registered in
Application_Start
:You probably might need to adjust some of the paths as it was not quite clear in your question where exactly should the js be located but normally you should have enough details in the answer.