Reputation: 10193
I want to serialise my model in my View, and to do so I use the following line;
var initialData = <%: new JavaScriptSerializer().Serialize(Model) %>;
The serialisation I need for my View to work properly is the following
var initialData = {"EmployeeList":[],"ClientEmployeeSelector":{"SearchText":null,"SearchTextId":0},"Cvm":null,"TrainingName":null,"TrainingDescription":null};
But what I am currently getting is;
var initialData = {"ClientEmployeeSelector":{"SearchText":null,"SearchTextId":0},"EmployeeList":[],"Cvm":null,"TrainingDescription":null,"TrainingName":null};
So instead of the quotation symbol " appearing, I get & quot; and this stops my view from working. How do I fix this?
Upvotes: 0
Views: 87
Reputation: 1062
You can return it as a MvcHtmlString and it will output without html-encoding the string.
var initialData = <%: new MvcHtmlString(new JavaScriptSerializer().Serialize(Model)) %>;
Upvotes: 0
Reputation: 9881
Using the colon syntax <%:
HTML-encodes the response. Use <%=
to write out the unencoded values.
var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;
Upvotes: 2