You should never use string concatenation when working with javascript. If you want to pass some server-side model to a javascript variable, you can JSON serialize it to ensure that dangerous characters are properly escaped:
var graphByMonth = @Json.Encode(Model.Cost.Select(cost => new { cpm = cost.CPM, endDate = cost.EndDate }));
which will be displayed as:
var graphByMonth = [ { cost: '1', endDate: 'date 1' }, { cost: '2', endDate: 'date 2' }, { cost: '3', endDate: 'date 3' } ];
Using Json.Encode , you will ensure that the values ββare correctly encoded, and you will not have any broken syntax that you get if you try to manually do this using some string concatenation, foreach loops or something else.
source share