arame3333
arame3333

Reputation: 10193

Cannot serialize my model in my View properly

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 = {&quot;ClientEmployeeSelector&quot;:{&quot;SearchText&quot;:null,&quot;SearchTextId&quot;:0},&quot;EmployeeList&quot;:[],&quot;Cvm&quot;:null,&quot;TrainingDescription&quot;:null,&quot;TrainingName&quot;: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

Answers (2)

truemedia
truemedia

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

Andy T
Andy T

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

Related Questions