In C#, I'm trying to convert a DataTable into a Javascript object through Razor syntax. However, when assigning string values to a Javascript key, the quote strings " get converted into HTML codes in Javascript \". How do I preserve the quote " in Javascript?
Example code:
<script type="text/javascript">
@{
string json = "";
foreach (DataRow x in Model.MyDataTable.Rows)
{
json += "{field1: \"" + x["field1"].ToString() + "\"},";
}
json = json.TrimEnd(',');
}
var table = [@json];
</script>
What I'm expecting to see in Javascript debugger:
[{field1: "0001"}, {field1: "0002"}]
What I'm getting:
[{field1: \"0001\"}, {field1: \"0002\"}]
I've tried to use HttpUtility.JavaScriptStringEncode() but this was returning a similar issue.