Question

I am serializing a list object using the JavaScriptSerializer and assigning the result to a javascript object but when i use the aspx engine the quotes in the json string are considered as double quotes and working correct but when using razor engine the double quotes are printed as " and through exception.

How to resolve this?

My sample code.

ASPX:

var data = <%=JsonConverter.ConvertDataTabletoString()%>
var data = [{"No":"1","Name":"John"},{"No":"2","Name":"Smith"},  {"No":"3","Name":"Tomps"},{"No":"4","Name":"Hanar"},{"No":"5","Name":"Reek"}]`

Razor:

var data = @JsonConverter.ConvertDataTabletoString()
var data = [{&quot;No&quot;:1,&quot;Name&quot;:&quot;Heer&quot;},{&quot;No&quot;:1,&quot;Name&quot;:&quot;Heer&quot;},{&quot;No&quot;:1,&quot;Name&quot;:&quot;Heer&quot;}]
Was it helpful?

Solution

Try using Html.Raw() to print out the code in the Razor view. That will stop the code from being HTML-escaped.

Eg:

@Html.Raw(JsonConverter.ConvertDataTabletoString())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top