1

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>   
  • 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
     }
    

2 Answers 2

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 [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>
Sign up to request clarification or add additional context in comments.

1 Comment

yes this bit works! but it says i cannot use <script> tag within <tr> tag..and i cant use it within a <td>..see update please
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.