Hi All,

I have MVC application with couple of controllers. I am trying to find efficient way to share DTO between these two to update the data in UI.

First controller "viewProfile" populate all fields in DTO for first time. UI use this DTO to display the user profile. In course of time user can update their preferences by calling "updateEventPreference". Now I am trying to find a way so that I can update initial DTO with new preferences without reloading profile page.

Any suggestion?

DTO
Java Code:
class ProfileDTO {
   private String name;
   private List<Event> event;

   class Event {
      Integer eventId
      String name
      Boolean preferred
   }   
}
Controller to load the profile page
Java Code:
@RequestMapping(value = "/profile")
public ModelAndView viewProfile(HttpServletRequest request) throws Exception {
   ProfileDTO profileDTO = legacyService.getProfile();
   ModelAndView modelAndView = new ModelAndView("profile");
   modelAndView.addObject("profileDTO", profileDTO);
   return modelAndView;
}
Controller to update event preference
Java Code:
@RequestMapping(value = "/updateEventPreference")
public ModelAndView updateEventPreference(HttpServletRequest request, @RequestParam List<Integer> preferredEventIDs) throws Exception {
   ModelAndView modelAndView = new ModelAndView("json"); //"json" configured to return json type
   try {
      legacySerice.updateEventPreference(preferredEventIDs);
   } catch(LegacyException ex {
      modelAndView.addObject("updated", false);
      return modelAndView;
   }   
   modelAndView.addObject("updated", true);
   return modelAndView;
}
AJAX call to update event preference
Java Code:
$.ajax({
   url: "/updateEventPreference.do",
   type: "POST",
   dataType: "json",
   data: { preferredEventIDs: preferEvenId },
   traditional: true		
}).done(function(response) {
   if(response.updated) {
      //do update in UI
   } else {
      alert("Update failed!!!");
   }
}).fail(function() { 
   alert("Update failed!!!");			
});