I have a JSON string returned from my asp.net mvc3 application controller using viewbag key as below, depending on various scenarios it can be null too
{"Name":"TShirt","Id":"85","Gender":"M","Size":"Medium"}
and I am trying to assign this JSON object to my local anonymous variable as shown below:
var data = '@ViewBag.UserData';
The above ViewBag is null on initial page load, but as the user keys in the criteria, I store this into a session variable. Whenever user selects a product and clicks on back to previous page link, I restore these filters froms session and place them in Viewbag for further processing on the View.
This is working perfectly fine when user clicks back to previous page , but I see a script error on the initial load because the var data = '@ViewBag.UserData'; evaluates to var data = ;
I tried the below too but am still seeing the error
var data = '@ViewBag.UserData' != '' ? @Html.Raw(@ViewBag.UserData): {};
I tried
var data = '@ViewBag.UserData' != '' ? '@Html.Raw(@ViewBag.UserData)': {};
but I am unable to parse the data that I get in the back to previous page scenario. Am I doing something wrong here?
var data = '' != '' ? : {};var data = '@ViewBag.UserData' != '' ? @Html.Raw(@ViewBag.UserData): {};var data = '@(ViewBag.UserData ?? Html.Raw(""))';