Esempio n. 1
0
        public ActionResult Edit(Guid id, MetaReportModel model, List <MetaReportItemModel> items, string button)
        {
            using (DBEntities context = Settings.CreateDataContext())
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                MetaReport target = null;
                if (model.Id != Guid.Empty)
                {
                    target = MetaReportHelper.Get(model.Id, context);
                    if (target == null)
                    {
                        ModelState.AddModelError("", "Запись не найдена!");
                        return(View(model));
                    }
                }
                else
                {
                    target    = new MetaReport();
                    target.Id = Guid.NewGuid();
                    context.AddToMetaReport(target);
                }

                MetaReportModel.CreateMap();
                Mapper.Map(model, target);

                if (items != null)
                {
                    SyncReportItems(target, items, context);
                }

                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 }));
                }
            }
        }