public ActionResult PostArithmetic(NewArithmeticModel model) { // ** Prototype pattern. the arithmetic object which has its default values set var arithmetic = Mapper.Map<NewArithmeticModel, Arithmetic>(model, new Arithmetic(true)); if (arithmetic.Id != 0) // existing arithmetic { if (ModelState.IsValid) { var dt = new DataTable(); arithmetic.Result = (int)dt.Compute(model.ArithmeticOperation, ""); if (!arithmetic.IsValid) // shows how custom validation would be used { // examine arithmetic.Errors list } ProViewContext.Arithmetics.Update(arithmetic); Success = "Biểu thức " + arithmetic.ArithmeticOperation + " được cập nhật thành công."; return RedirectToAction("Arithmetics"); } } else // new arithmetic { if (ModelState.IsValid) { try { var dt = new DataTable(); arithmetic.Result = (double)dt.Compute(model.ArithmeticOperation, ""); // ** Facade pattern. Unit of Work pattern. ProViewContext.Arithmetics.Insert(arithmetic); Success = "Biểu thức " + arithmetic.ArithmeticOperation + " được thêm mới thành công."; return RedirectToAction("Arithmetics"); } catch (Exception ex) { Failure = ex.Message; } } } return View(model); }
public ActionResult GetArithmetic(int? id = null) { var model = new NewArithmeticModel(); if (id != null) { var arithmetic = ProViewContext.Arithmetics.Single(id); model = Mapper.Map<Arithmetic, NewArithmeticModel>(arithmetic); } model.HttpReferer = Request.UrlReferrer.PathAndQuery; return View("Arithmetic", model); }