I have a collection that I would like to iterate over, which is held in a C# variable.
I would like to iterate over this using a for loop from within Javascript; is this possible?
For example:
@{ int questionCount = 0;
foreach(var q in Model.Questions){
questionCount++;
}
...do some HTML...
<script type="text/javascript">
jQuery(function () {
for (var i=1;i<=@questionCount;i++)
{
var d1 = @Model.Questions[i].Answers.Where(m => m.Answer == 1).Count();
<-- this is where it breaks -->
}
});
The error I get says that the index is out of range. I guess this means that it doesn't understand 'i' once I've put the @ symbol into the line. How can I delimit the string correctly to use the variable i (from Javascript) to iterate over my C# collection?