public JsonResult Save(EntityTypeViewModel vm) { if (ModelState.IsValid) { CFEntityType model; if (vm.Id > 0) { model = EntityTypeService.GetEntityTypeById(vm.Id);//Db.EntityTypes.Where(x => x.Id == vm.Id).FirstOrDefault(); if (model == null) { return(Json(vm.Error("Specified entity type not found"))); } else { vm.UpdateDataModel(model, Db); //Db.Entry(model).State = System.Data.Entity.EntityState.Modified; EntityTypeService.UpdateEntityType(model); } } else { model = new CFEntityType(); vm.UpdateDataModel(model, Db); // Db.EntityTypes.Add(model); EntityTypeService.UpdateEntityType(model); } Db.SaveChanges(User.Identity); vm.Status = KoBaseViewModel.eStatus.Success; if (vm.Id == 0) { //This is a newly created object, so we ask knockout MVC to redirect it to the edit page //so that the ID is added to the URL. vm.redirect = true; vm.url = Url.Action("Edit", "EntityTypes", new { id = model.Id }); } } else { if (string.IsNullOrEmpty(vm.Name)) { vm.ErrorMessage = "*"; } foreach (var att in vm.AttributeMappings) { if (string.IsNullOrEmpty(att.Name) || string.IsNullOrEmpty(att.Field)) { att.ErrorMessage = "*"; } } return(Json(vm.Error("Model validation failed"))); } return(Json(vm)); }