public async Task UpdateModel(ViewModel vm, string data, string props, string method, JToken[] parameters, HttpContext context, TextWriter writer) { var jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Include, ObjectCreationHandling = ObjectCreationHandling.Replace, ContractResolver = new CustomContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }; // populate my object with client $data JsonConvert.PopulateObject(data, vm); // populate my object with client $props JsonConvert.PopulateObject(props, vm); // parse $data as original value (before any update) var original = JObject.FromObject(vm, jsonSerializer); try { // set viewmodel request data ViewModel.SetData(vm, original); // if has method, call in existing vms var result = this.ExecuteMethod(method, vm, parameters, context.Request.Form.Files, context.RequestServices); // now, get viewmodel changes on data var current = JObject.FromObject(vm, jsonSerializer); // merge all scripts var scripts = ViewModel.GetClientScript(vm); // detect changed from original to current data and send back to browser var diff = this.GetDiff(original, current); // write changes to writer using (var w = new JsonTextWriter(writer)) { var output = new JObject { { "update", diff }, { "script", scripts }, { "result", result == null ? null : JToken.FromObject(result) } }; await output.WriteToAsync(w); } } finally { // dispose vm vm.Dispose(); } }
public async Task UpdateModel(ViewModel vm, string data, string props, string method, JToken[] parameters, IFormFileCollection files, TextWriter writer) { // populate my object with client $data JsonConvert.PopulateObject(data, vm, Config.JsonSettings); // populate my object with client $props JsonConvert.PopulateObject(props, vm, Config.JsonSettings); // parse $data as original value (before any update) var original = JObject.FromObject(vm); try { // set viewmodel request data ViewModel.SetData(vm, original); // if has method, call in existing vms var result = this.ExecuteMethod(method, vm, parameters, files); // now, get viewmodel changes on data var current = JObject.FromObject(vm, Config.JsonSerializer); // merge all scripts var scripts = ViewModel.GetClientScript(vm); // detect changed from original to current data and send back to browser var diff = this.GetDiff(original, current); // write changes to writer using (var w = new JsonTextWriter(writer)) { var output = new JObject { { "update", diff }, { "script", scripts }, { "result", result == null ? null : JToken.FromObject(result) } }; await output.WriteToAsync(w); } } finally { // dispose vm vm.Dispose(); } }