public JsonResult Post(string consignorCode, int eventNumber, [FromBody] ContractDetailViewModel viewModel) { try { if (ModelState.IsValid) { var contractHeader = _repository.GetContractHeaderByCosignorCodeAndEventNumber(consignorCode, eventNumber); if (contractHeader == null) { return(Json($"Error - Contract Header not found for Consignor->{consignorCode} and Event->{eventNumber}")); } // Map to the Entity var newDetail = Mapper.Map <ContractDetail>(viewModel); //Save to Database _repository.AddContractDetail(consignorCode, eventNumber, newDetail); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <ContractDetailViewModel>(newDetail))); } } } catch (Exception ex) { _logger.LogError("Failed to save new contracts detail", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = ex.Message, Text = "Did not work!" })); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Model Validation Failed!")); }