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 }