public ActionResult Customers_Update([DataSourceRequest] DataSourceRequest request, CustomerViewModel customerViewModel) { db.DisableAllFilters(); if (ModelState.IsValid) { using (var db = new NovaAdminContext()) { Customer entity = db.Customers.Include(customer => customer.Address).FirstOrDefault(c => c.Id == customerViewModel.Id); if (entity == null) { string errorMessage = string.Format("Cannot update record with OrderID:{0} as it's not available.", customerViewModel.Id); ModelState.AddModelError("", errorMessage); } else { CustomerViewModelCustomerMapper.MapCustomerForUpdate(customerViewModel, entity, entity.Address); db.Customers.Attach(entity); db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); } } } return(Json(new[] { customerViewModel }.ToDataSourceResult(request, ModelState))); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Name,DateOfBirth,Gender,Category,HouseNumber,AddressLine1")] CustomerViewModel customerViewModel) { if (ModelState.IsValid) { using (var db = new NovaAdminContext()) { Customer entity = await db.Customers.Include(customer => customer.Address).FirstOrDefaultAsync(c => c.Id == customerViewModel.Id); if (entity == null) { string errorMessage = string.Format("Cannot update record with OrderID:{0} as it's not available.", customerViewModel.Id); ModelState.AddModelError("", errorMessage); } else { CustomerViewModelCustomerMapper.MapCustomerForUpdate(customerViewModel, entity, entity.Address); db.Customers.Attach(entity); db.Entry(entity).State = EntityState.Modified; db.SaveChangesAsync(); return(RedirectToAction("Index")); } } } return(View(customerViewModel)); }