title: Best practices for passing data from asp.net-mvc to javascript
body: <p>I've been working with ASP.NET MVC and Javascript/jQuery a lot lately and I seem to be heading in a direction where I'm always needing to pass some dynamic value "to" my javascript. When the script is right in the page, I've done something like this:</p>
  
  <pre><code>var isEditable = &lt;%=ViewData["editable"]%&gt;
  </code></pre>
  
  <p>I like how this is quick &amp; easy and just like I'd inject a value into HTML. But this smells. Really, really bad.  And it breaks Visual Studio's intellisense and code formatting, making my scripts hard to read and understand.</p>
  
  <p>It has occurred to me another solution would be to pass my data to a hidden field, and have the Javascript reference that...</p>
  
  <pre><code>&lt;input type="hidden" id="editable" value="&lt;%=ViewData["editable"]%&gt;" /&gt;
  var isEditable = $("#editable").attr("value");
  </code></pre>
  
  <p>This is probably much better as it keeps the script intact and would allow me to move it to an external .js file. But something about this solution doesn't seem ideal either. Or is it just me?</p>
  
  <p>Can anyone recommend solutions &amp; best practices for passing data into your scripts? Am I headed down the wrong path if my scripts end up heavily relying the viewdata from my controllers in the first place?</p>