Samra Samra - 1 year ago 41
Javascript Question

access each element in a data set from javascript

I want to access each element in a data set from javascript. I want to do something like the following in my javascript.

for(var i = 0; i < @Model.saVM.mapDetails.Count; i++){
var data = @Model.saVM.mapDetails;

But i keep getting this error

Unterminated template literal

Other things i have tried..

for(var i = 0; i < @Model.saVM.mapDetails.Count; i++){
var data = @Model.saVM.mapDetails.ElementAt(i);



for(var i = 0; i < @Model.saVM.mapDetails.Count; i++){
var data = @Model.saVM.mapDetails.ElementAt(@:i);


Here is why i need to do this

I have an MVC application that binds data from Model into an html table. The number of rows and columns are variable depending on the data in Model.

public class StudentAssessmentViewModel
public StudentAssessmentViewModel() { }
public List<ShortResult> results { get; set; }
public List<ShortStudent> students {get; set;}
public List<MapDetail> mapDetails { get; set; }
public List<ResultType> resultTypes { get; set; }
public List<ShortResult> calculatedResults { get; set; }
public List<Mean> means { get; set; }

now in this table, when i create a td, i check if there is value and color in Model.results (that matches current student (in row) AND current Header (in column)) to be put in current td, if yes then put the value and color else move forward i.e. create next td.

Then i converted this table into a kendo grid, which preserved the td values but removed all the colors.

So now i have to separately parse results list and again assign colors to each cell in kendogrid.

Answer Source

To loop you need to do as below:

var model = @Html.Raw(Json.Encode(@Model.saVM.mapDetails));
$(model).each(function (index, item) {

Assuming you have MapId and MapName properties in mapDetails. Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download