title: Using javascript/jquery from within razor code
body: <p>It seems simple but i cant figure out how to call javascript function from within razor code.</p>
  
  <p>Problem: I need to get the position of a column in my table header with the id passed.. I am calling <strong>getPosition</strong> function from within my razor code </p>
  
  <pre><code>&lt;table&gt;
     &lt;thead&gt;
         &lt;tr&gt;
         @foreach (Assessment geAssessment in Model.assessments)
         {
            &lt;th id=@geAssessment.AssessmentID&gt;@geAssessment.Name&lt;/th&gt;
         }
         &lt;/tr&gt;
     &lt;/thead&gt;
     &lt;tbody&gt;
       &lt;tr&gt;
         @foreach (ShortResult geResult in Model.results)
         {
             @:{ var i = getPosition(@geResult.assessmentId);}
         }
        &lt;/tr&gt;
    &lt;/tbody&gt;
   &lt;/table&gt;   
  </code></pre>
  
  <ul>
  <li><p>My script in the same view/page</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
     function getPosition(id) {
      var c = '#' + id;
      alert(c);
      return $c.index();    
    }
  
    $(function () {});
  &lt;/script&gt;
  </code></pre></li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As suggested by Max, i changed my table as follows, which is perfect but now how can i set a value in the td</p>
  
  <pre><code>&lt;tbody&gt;
    &lt;tr&gt;
      @{
                                      var index = 4; //start index of assessments will be 4
                                      foreach(Assessment geAssessment in Model.assessments)
                                      {
                                      &lt;td&gt;
                                          @foreach (ShortResult geResult in Model.results)
                                          {
                                              if(geResult.StudentID == geStudent.studentid)
                                              {
                                              @:
                                                  &lt;script&gt;
                                                      {
  
                                                          var assessmentIndex = getPosition(@geResult.assessmentId);
                                                          @*if (assessmentIndex == @index) {
                                                              geResult.ResultValue
                                                          }*@
  
                                                      }
                                                  &lt;/script&gt;
  
                                              }
                                          }
                                       &lt;/td&gt;
                                      index++;
                                      }
                                   }
    &lt;/tr&gt;    
   &lt;/tbody&gt;
  </code></pre>
  
  <p>Now let me explain whats going on ..</p>
  
  <ul>
  <li>i want to add x tds in a row based on the number of assessments in my model</li>
  <li><p>for each td i check if i have a result with this particular assessmentid, if yes i want to print it in the td..there is some syntax error here:</p>
  
  <pre><code>if (assessmentIndex == @index) {
                                                              geResult.ResultValue
   }
  </code></pre></li>
  </ul>