I am having an issue pushing a list from my C# model in razor to jquery to play with!
So assume my List is working well and I am able to see the data in razor (Because I can)
I then have the following:
@using Models.DatabaseModel;
@using Newtonsoft.Json;
@{
Person _Person = ViewBag._Person;
var json = JsonConvert.SerializeObject(new
{
operations = _Person
});
}
And then at the bottom I have a javascript loading as so:
<script type="text/javascript">
$(document).ready(function () {
StoreUserInfo("@json");
});
</script>
The following calls the javascript correctly, but the string that is passed through to the javascript is full of html characters
An example: "{"operations":{"PersonID":"8"}}"
My jquery is as follows:
function StoreUserInfo(UserObject)
{
var jsonobj = $.parseJSON(UserObject);
if (typeof (Storage) !== "undefined") {
} else {
}
}
Which errors with the Syntax Error:
Uncaught SyntaxError: Unexpected token & in JSON at position 1
The theory behind this is my C# list pushes a heap of data that I want to store in local storage, I am open to a better method to do this if anyone has it or solutions to my syntax issue I am having.
UserObjectis already contains a JSON object, where$.parseJSONcallstoStringmethod which reveals&sign at first place (possibly by") causing the problem. Can you provide sample of passed string to JS side?