Hi everyone,
I need help you because I am using .Net6 and I have a ViewData for transfer data from controller to view.
I don't understand this error in frontend.
Uncaught SyntaxError: Invalid or unexpected token
using Newtonsoft.Json;
//IMemoryCache cache
private const string _currencyCodes = "CURRENCY_CODES";
public ActionResult Index()
{
if (_cache.Get(_currencyCodes) == null)
{
//GetCurrencyCodesMC execute a stored procedure
var datosCurrency = ad.GetCurrencyCodesMC().Tables[0].Select().Select((x) => new
{
Id = x.Field<int?>("Id"),
Name = x.Field<string>("Name"),
Code = x.Field<int?>("Code"),
Alpha_3 = x.Field<string>("Alpha_3"),
Exponent = x.Field<decimal?>("Exponent")
}).ToArray();
string result = JsonConvert.SerializeObject(datosCurrency, Formatting.Indented);
/*
This contains the result variable
"[\r\n {\r\n \"Id\": 1,\r\n \"Name\": \"cur1\",\r\n \"Code\": 6,\r\n \"Alpha_3\": \"cur2\",\r\n \"Exponent\": 0.0\r\n },\r\n {\r\n \"Id\": 2,\r\n \"Name\": \"cur3\",\r\n \"Code\": 8,\r\n \"Alpha_3\": \"cur3\",\r\n \"Exponent\": 1.0\r\n }\r\n]"
*/
_cache.Set(_currencyCodes, result, cacheEntryOptions);
}
ViewData[_currencyCodes] = _cache.Get(_currencyCodes);
return View();
<script>
var result = '@Html.Raw(@ViewData["CURRENCY_CODES"].ToString())';
var CURRENCY_CODES = JSON.parse(result);
</script>
<script>
var aux1 = '[ *Invalid or unexpected token
{
"Id": 1,
"Name": "cur1",
"Code": 6,
"Alpha_3": "cur2",
"Exponent": 0.0
},
{
"Id": 2,
"Name": "cur3",
"Code": 8,
"Alpha_3": "cur3",
"Exponent": 1.0
}
]';
</script>
Thanks