title: MVC: Calling Javascript function in View without any HTML control
body: <p>I have a view in which I do some condition check when the page load. </p>
  
  <pre><code> if(model.count()&gt;0){
  
   }
  </code></pre>
  
  <p>I then want to call a javascript function if this condition is satisfied. How can I do this please?</p>
  
  <p>I know how to do that within an html control but can this be done without any control?</p>
  
  <p>Or how can i do this check in the javascript, thus having to refer to the view's model?</p>
  
  <p>EDIT</p>
  
  <p>This is what I have now, but the function is not recognized: <code>The name myfunction does not exist in the current context</code></p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
          @if (Model.Count() &gt; 0)
          {
             myfunction(Model.parameter);
  
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>EDIT</p>
  
  <p>I changed that to</p>
  
  <pre><code>@if (Model.Count() &gt; 0)
  {
  &lt;script type="text/javascript"&gt;
  
          $(document).ready(function () {
          myfunction(@Model.parameter);
       });
  
  &lt;/script&gt;
   }
  </code></pre>
  
  <p>But not working. Even if I try something like <code>alert("test" + @Model.parameter)</code> within the .ready(function()), it does not work.</p>
  
  <p>Alternatively, I will have to write the code in the view.  I tried eliminating the function and doing it in the view but i will need a way of setting the id property of html element to a variable. something like <code>var variable = Model.param1</code> and then having <code>&lt;div id=variable&gt;&lt;/div&gt;</code>. But how can I set the id property to a variable?</p>