title: ASP.NET MVC5 Call JavaScript function from Partial View
body: <p>I want to call JavaScript function, to which I pass parameter, from MVC5 Partial view.</p>
  
  <p>I have declared the function <code>addItem(item)</code> in separate JS file at the top of the file, before <code>$(document).ready(function {})</code> function.</p>
  
  <p>I have tried the following code:</p>
  
  <pre><code>@foreach (string item in (TempData["Items"] as List&lt;string&gt;))
  {
      TempData["Item"] = item;
      &lt;script type="text/javascript"&gt;addItem(@TempData["Item"]);&lt;/script&gt;
  }
  </code></pre>
  
  <p>And the following:</p>
  
  <pre><code>@foreach (string item in (TempData["Items"] as List&lt;string&gt;))
  {
      &lt;script type="text/javascript"&gt;addItem(item);&lt;/script&gt;
  }
  </code></pre>
  
  <p>both tries end up with the following error: <code>Uncaught ReferenceError: theValueOfMyItem is not defined</code>.</p>
  
  <p>I even tried this:</p>
  
  <pre><code>@foreach (string item in (TempData["Items"] as List&lt;string&gt;))
  {
      addItem(item);
  }
  </code></pre>
  
  <p>but as expected, this gives me error <code>The name 'addItem' does not exist in the current context</code>.</p>
  
  <p>So my question is:</p>
  
  <p>Can I call JS function with parameter from partial view, and if possible what is the proper way of doing this?</p>
  
  <h2>Edit 1</h2>
  
  <p>After the suggestions of <em>Carsten Løvbo Andersen</em> and <em>Satpal</em> I get the following error: <code>Uncaught TypeError: addItem is not a function</code>.</p>
  
  <p>Here is how I defined the function:</p>
  
  <pre><code>function addItem(item) {
      // processing...
  }
  </code></pre>