public ActionResult Edit(Guid id, MetaSchedulerModel model, string button) { using (DBEntities context = Settings.CreateDataContext()) { Validate(context, model); if (!ModelState.IsValid) { return View(model); } MetaScheduler target = null; if (model.Id != Guid.Empty) { target = MetaSchedulerHelper.Get(model.Id, context); if (target == null) { ModelState.AddModelError("", "Запиcь не найдена!"); return View(model); } } else { target = new MetaScheduler(); target.Id = Guid.NewGuid(); context.AddToMetaScheduler(target); } MetaSchedulerModel.CreateMap(); Mapper.Map(model, target); try { context.SaveChanges(); } catch (Exception ex) { var sb = new StringBuilder(Resources.Resource.SaveError + ": " + ex.Message); if (ex.InnerException != null) sb.AppendLine(ex.InnerException.Message); ModelState.AddModelError("", sb.ToString()); return View(model); } if (button == "SaveAndExit") return RedirectToAction("Index"); else return RedirectToAction("Edit", new {target.Id}); } }
private void Validate(DBEntities context, MetaSchedulerModel model) { if (model.TypeId == 0 && model.PeriodId.HasValue) { if (model.PeriodId.Value > 0 && !model.StartTime2.HasValue) { ModelState.AddModelError("StartTime2", "Поле обязательно для заполения!"); } if (model.PeriodId.Value > 1 && !model.NumberInPeriod.HasValue) { ModelState.AddModelError("NumberInPeriod", "Поле обязательно для заполения!"); } } if (model.TypeId == 1 && !model.IntervalSecond.HasValue) { ModelState.AddModelError("IntervalSecond", "Поле обязательно для заполения!"); } }