title: How to elegantly escape single and double quotes when passing C# string to javascript
body: <p>I have a C# string that needs to be passed to the page and then sent back to the server. It needs to be able to handle single quotes, double quotes and any other standard characters that might cause problems. I'm using MVC4 and razor. Right now I'm passing the string to javascript. I have something like this:</p>
  
  <pre><code>var str = '@Html.Raw(Model.SomeString.Replace("'", @"\'"))';
  </code></pre>
  
  <p>This works just fine but I was wondering if there's a better, more elegant way of escaping a C# string that will be passed to javascript.</p>
  
  <p>I've already tried storing the value in a hidden field, like this:</p>
  
  <pre><code>@Html.Hidden(x =&gt; x.SomeString)
  </code></pre>
  
  <p>but then it has problems with double quotes when I go to grab the value.<br>
  <strong>EDIT: turns out the Html.Hidden issue was being caused by something else</strong></p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>NOTE: must work in IE9 and 10. No ViewBags.</p>