public ActionResult Edit(InvoiceViewModel model) { if (ModelState.IsValid) { bool isCreateNew = false; Invoice fromEntity = model.Invoice; Invoice toEntity = null; if (model.Id == WebUiConstants.ID_FOR_CREATE_NEW_ENTITY) { isCreateNew = true; toEntity = new Invoice(); } else { toEntity = _InvoiceRepository.GetById(model.Id); if (toEntity == null) { return(new BadRequestObjectResult( String.Format("Unknown client id '{0}'.", model.Id))); } } if (fromEntity == null || toEntity == null) { return(new BadRequestObjectResult("fromEntity or toEntity was null")); } else { toEntity.OwnerClientIDFK = Int32.Parse(model.ClientId); var adapter = new InvoiceAdapter(); adapter.Adapt(fromEntity, toEntity); _InvoiceRepository.Save(toEntity); } if (isCreateNew == true) { model.Id = toEntity.Id; return(RedirectToAction("Edit", new { id = toEntity.Id })); } else { return(RedirectToAction("Edit")); } } else { return(View(model)); } }