title: how to escape an apostrophe in asp.net-mvc and javascript
body: <p>I have the following code in my controller:</p>
  
  <pre><code>    public ActionResult MyPage()
      {
        var vm = new MyViewModel();
        vm.StringList = "'" + String.Join("','", MyModel.GetList().Select(r =&gt; r.Name).ToArray()) + "'";
  
          return View(vm);
      }
  </code></pre>
  
  <p>and in my asp.net-mvc view i have the following code:</p>
  
  <pre><code> var myJavascriptArray = [&lt;%=Model.StringList %&gt;];
  </code></pre>
  
  <p>inside a javascript function</p>
  
  <p>this works fine until one of the names in the:</p>
  
  <pre><code> MyModel.GetList()
  </code></pre>
  
  <p>call has a apostrophe in it like   <strong>"Bill O'Brien"</strong></p>
  
  <p>as that messes up my javascript code as it thinks the ' after the O is the end of that entry and the line breaks.</p>
  
  <p>What is the best way to escape out of this character so i can have an array in javascript like that will ultimately show up as this:</p>
  
  <p>var myJavascriptArray = [''Item 1',Item 2','Item 3','Item 4'];</p>
  
  <p>and deal with <strong>'Bill O'Brien'</strong> being one of the entries.</p>