Using javascript/jquery from within razor code

2019-05-30 18:39发布

问题:

It seems simple but i cant figure out how to call javascript function from within razor code.

Problem: I need to get the position of a column in my table header with the id passed.. I am calling getPosition function from within my razor code

<table>
   <thead>
       <tr>
       @foreach (Assessment geAssessment in Model.assessments)
       {
          <th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
           @:{ var i = getPosition(@geResult.assessmentId);}
       }
      </tr>
  </tbody>
 </table>   
  • My script in the same view/page

    <script type="text/javascript">
       function getPosition(id) {
        var c = '#' + id;
        alert(c);
        return $c.index();    
      }
    
      $(function () {});
    </script>
    

UPDATE

As suggested by Max, i changed my table as follows, which is perfect but now how can i set a value in the td

<tbody>
  <tr>
    @{
                                    var index = 4; //start index of assessments will be 4
                                    foreach(Assessment geAssessment in Model.assessments)
                                    {
                                    <td>
                                        @foreach (ShortResult geResult in Model.results)
                                        {
                                            if(geResult.StudentID == geStudent.studentid)
                                            {
                                            @:
                                                <script>
                                                    {

                                                        var assessmentIndex = getPosition(@geResult.assessmentId);
                                                        @*if (assessmentIndex == @index) {
                                                            geResult.ResultValue
                                                        }*@

                                                    }
                                                </script>

                                            }
                                        }
                                     </td>
                                    index++;
                                    }
                                 }
  </tr>    
 </tbody>

Now let me explain whats going on ..

  • i want to add x tds in a row based on the number of assessments in my model
  • 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:

    if (assessmentIndex == @index) {
                                                                geResult.ResultValue
     }
    

回答1:

Your script with getPosition function has to be above your call. After @: you have to use directive otherwise it's considered like a text. Your code has to look like:

<script type="text/javascript">
  function getPosition(id) {
    var c = '#' + id;
    alert(c);
    return $c.index();    
  }

  $(function () {});
</script>


<table>
   <thead>
       <tr>
       @foreach (Assessment geAssessment in Model.assessments)
       {
          <th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
           @: <script>{ var i = getPosition(@geResult.assessmentId);}</script>
       }
      </tr>
  </tbody>
 </table>


回答2:

Hi there is no need of @: syntax you can directly use it so now your code look like.

 <script type="text/javascript">
  function getPosition(id) {
    var c = '#' + id;
    alert(c);
    return $c.index();    
  }

  $(function () {});
</script>

<table>
   <thead>
       <tr>
       @foreach (Assessment geAssessment in Model.assessments)
       {
          <th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
        <script>{ var i = getPosition(@geResult.assessmentId);}</script>
       }
      </tr>
  </tbody>
 </table>