Monday, November 21, 2011

Serialize objects to get them across the seam between C# and jQuery.

An audit of the work I've done here and here by Joel and Jorge has been forgiving and has largely found that I wasn't too off the mark in my implementation. Obviously, I should have used push to append items into arrays on the jQuery side. Another shortcoming is that I have not serialized objects when attempting to cross the seam between C# and jQuery. I learned how to do so today however. One may put a serialized object into string like so...

string Foo = Newtonsoft.Json.JsonConvert.SerializeObject(myArray);

 
 

...and then retrieve it, unflattened, in jQuery like so...

var flatPrograms = @Html.Raw(Model.Foo);

 
 

...JSON is used as the translation format. Serialize objects to get them across the seam between C# and jQuery. Do not flatten the objects on your own in C# and then unflatten them in jQuery. Duh. This is the better way to move collections across the divide.

No comments:

Post a Comment