Guest User

Untitled

a guest
Aug 14th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. Picking out Just JSON Data Returned from ASP.NET MVC3 controller Update
  2. [HttpPost]
  3. public JsonResult Update(President data)
  4. {
  5. bool success = false;
  6. string message = "no record found";
  7. if (data != null && data.Id > 0)
  8. {
  9. using (var db = new USPresidentsDb())
  10. {
  11. var rec = db.Presidents.FirstOrDefault(a => a.Id == data.Id);
  12. rec.FirstName = data.FirstName;
  13. db.SaveChanges();
  14. success = true;
  15. message = "Update method called successfully";
  16. }
  17. }
  18.  
  19. return Json(new
  20. {
  21. data,
  22. success,
  23. message
  24. });
  25. }
  26.  
  27. rec.FirstName = data.FirstName ?? rec.FirstName;
  28.  
  29. if (data.FirstName != null)
  30. rec.FirstName = data.FirstName
  31. .
  32. .
  33. .
  34.  
  35. public static void CopyOnlyModifiedData<T>(T source, ref T destination)
  36. {
  37. foreach (var propertyInfo in source.GetType().GetProperties())
  38. {
  39. object value = propertyInfo.GetValue(source, null);
  40. if (value!= null && !value.GetType().IsValueType)
  41. {
  42. destination.GetType().GetProperty(propertyInfo.Name, value.GetType()).SetValue(destination, value, null);
  43. }
  44. }
  45. }
  46.  
  47. CopyOnlyModifiedData<President>(data, ref rec);
Add Comment
Please, Sign In to add comment