Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Picking out Just JSON Data Returned from ASP.NET MVC3 controller Update
- [HttpPost]
- public JsonResult Update(President data)
- {
- bool success = false;
- string message = "no record found";
- if (data != null && data.Id > 0)
- {
- using (var db = new USPresidentsDb())
- {
- var rec = db.Presidents.FirstOrDefault(a => a.Id == data.Id);
- rec.FirstName = data.FirstName;
- db.SaveChanges();
- success = true;
- message = "Update method called successfully";
- }
- }
- return Json(new
- {
- data,
- success,
- message
- });
- }
- rec.FirstName = data.FirstName ?? rec.FirstName;
- if (data.FirstName != null)
- rec.FirstName = data.FirstName
- .
- .
- .
- public static void CopyOnlyModifiedData<T>(T source, ref T destination)
- {
- foreach (var propertyInfo in source.GetType().GetProperties())
- {
- object value = propertyInfo.GetValue(source, null);
- if (value!= null && !value.GetType().IsValueType)
- {
- destination.GetType().GetProperty(propertyInfo.Name, value.GetType()).SetValue(destination, value, null);
- }
- }
- }
- CopyOnlyModifiedData<President>(data, ref rec);
Add Comment
Please, Sign In to add comment