public ActionResult Edit(CalculatorConstantViewModel viewModel) { if (Request.IsAjaxRequest()) { var req = new SaveCalculatorConstantRequest(); req.Id = viewModel.Id; req.Value = viewModel.Value; req.IsAjaxRequest = true; var response = _calculatorConstantService.SaveCalculatorConstant(req); return Json(response); } else { var req = viewModel.MapTo<SaveCalculatorConstantRequest>(); _calculatorConstantService.SaveCalculatorConstant(req); return RedirectToAction("Index"); } }
public SaveCalculatorConstantResponse SaveCalculatorConstant(SaveCalculatorConstantRequest request) { try { if (request.IsAjaxRequest) { var calculatorConstant = DataContext.CalculatorConstants.First(x => x.Id == request.Id); calculatorConstant.Value = request.Value; } else { if (request.Id == 0) { var calculatorConstant = request.MapTo<CalculatorConstant>(); DataContext.CalculatorConstants.Add(calculatorConstant); } else { var calculatorConstant = DataContext.CalculatorConstants.First(x => x.Id == request.Id); request.MapPropertiesToInstance<CalculatorConstant>(calculatorConstant); } } DataContext.SaveChanges(); return new SaveCalculatorConstantResponse { IsSuccess = true, Message = "Calculator Constant Has been saved" }; } catch (InvalidOperationException ex) { return new SaveCalculatorConstantResponse { IsSuccess = false, Message = ex.Message }; } }