title: Including script specific to an ASP.NET MVC4 view or partial view
body: <p>I've looked at a number of questions similar to <a href="https://stackoverflow.com/questions/14114084/how-to-add-a-script-in-a-partial-view-in-mvc4">How to add a script in a partial view in MVC4?</a> and <a href="https://stackoverflow.com/questions/11257940/mvc4-partial-view-javascript-bundling-issue">MVC4 partial view javascript bundling Issue</a> and am still struggling to understand ASP.NET MVC architecture when it comes to view-specific script. It seems the answer to others who have tried to include script in their MVC4 partial views is to put the script at a higher level. But some script can't be moved to a higher level where it will run more globally. For example, I don't want to run script that applies knockout.js data bindings for a view model whose controls aren't loaded. And I don't want to run a whole bunch of script for a whole bunch of views that aren't active every time I load a page.</p>
  
  <p>So I started using the view-specific <code>@Section Script</code> blocks in my <code>.vbhtml</code> views to include script specific to a view. However, as pointed out by others, this does not work in a partial view. I am prototyping our architecture to see what we can and can't do here. I'd like to think that I might be able, in some cases, to use a view as a partial view and vice versa. But when you pull in a view to use as a partial view the <code>@Section Script</code> block does not render. I have managed to get all my viewmodel script defined globally in a way such that I need only run one line of code to create and bind a view model, but I still need that one line of code to run only when a particular view is active. Where can I appropriately add this line of code in a partial view?</p>
  
  <pre><code>ko.applyBindings(window.webui.inventoryDetailViewModel(ko, webui.inventorycontext));
  </code></pre>
  
  <p>Am I going down the right path here? Is this a proper way to architect an MVC application?</p>
  
  <p><strong>Edit</strong> Found this question very closely related to my problem, and includes a significant part of my answer: <a href="https://stackoverflow.com/questions/7342814/knockoutjs-ko-applybindings-to-partial-view">Can you call ko.applyBindings to bind a partial view?</a></p>