I have the following Action method.
public ActionResult Action()
{
var model = new SampleViewModel();
model.JsonString = ReadJsonStringFromSomewhere();
return ViewResult(model);
}
In my view I have the following method to initialize a javascript variable.
<script type="text/javascript">
var jsObject = eval("(" + <%= Model.JsonString %> + ")");
alert(jsObject);
</script>
The ‘jsObject’ I get is undefined. What is wrong here. Also is it the best method to initialize a javascript variable with a complex json string?
JSON is literal JavaScript. You don’t need the
evalat all. The following will work:That said, your
evalversion should still work, albeit more slowly. You don’t show your JSON, but it might not be valid.