Samra
Samra

Reputation: 2015

Using javascript/jquery from within razor code

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 [email protected]>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
           @:{ var i = getPosition(@geResult.assessmentId);}
       }
      </tr>
  </tbody>
 </table>   

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 ..

Upvotes: 1

Views: 219

Answers (2)

Prashant-Systematix
Prashant-Systematix

Reputation: 269

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 [email protected]>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
        <script>{ var i = getPosition(@geResult.assessmentId);}</script>
       }
      </tr>
  </tbody>
 </table>

Upvotes: 1

Max
Max

Reputation: 1090

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 [email protected]>@geAssessment.Name</th>
       }
       </tr>
   </thead>
   <tbody>
     <tr>
       @foreach (ShortResult geResult in Model.results)
       {
           @: <script>{ var i = getPosition(@geResult.assessmentId);}</script>
       }
      </tr>
  </tbody>
 </table>

Upvotes: 1

Related Questions