My company is currently working on a consulting project that uses ASP.NET MVC and a lot of jQuery and JSON behind the scenes for data transfer which is a lot of fun. I’m personally tasked with the back-end processes but also handling user interface updates as JSON data is received. I have a form with several different controls in it that are updated once JSON is returned from a controller action and the controls are updated using jQuery selectors. Something like this:
var prefix = '#OfficeProfile_';
var addrPrefix = '#OfficeProfileAddress_';
for (prop in json.OfficeProfile)
var propVal = json.OfficeProfile[prop];
if (prop == 'Address') //Object property within JSON..loop through Address object properties
for (addrProp in propVal)
$(addrPrefix + addrProp).val(propVal[addrProp]).convertNullToEmptyString();
$(prefix + prop).val(propVal).convertNullToEmptyString();
The json variable in the previous code represents the JSON returned from the call to the server and it exposes an OfficeProfile property. The code loops look through each property in the OfficeProfile object, stores the value and then uses the property name to locate the appropriate control within the form that I want to update (using jQuery selector syntax). I suspect there’s additional refactoring that I could apply but has worked out nicely so far.
For more information about onsite, online and video training, mentoring and consulting solutions for .NET, SharePoint or Silverlight please visit http://www.thewahlingroup.com/.