コード例 #1
0
        public ActionResult SaveOrEdit(Content content, int[] selectedFileId = null, int[] selectedLabelId = null)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (content.CategoryId == 0)
                    {
                        var labels                 = new List <LabelLine>();
                        var fileManagers           = new List <FileManager>();
                        int mainImageFileManagerId = 0;
                        if (content.Id > 0)
                        {
                            content      = ContentRepository.GetContentWithFiles(content.Id);
                            labels       = LabelLineRepository.GetLabelLinesByItem(content.Id, ContentType);
                            fileManagers = content.ContentFiles.Select(r => r.FileManager).ToList();
                            var mainImage = content.ContentFiles.FirstOrDefault(r => r.IsMainImage);
                            if (mainImage != null)
                            {
                                mainImageFileManagerId = mainImage.FileManagerId;
                            }
                        }
                        ViewBag.MainImageId    = mainImageFileManagerId;
                        ViewBag.SelectedLabels = labels.Select(r => r.LabelId).ToArray();
                        ViewBag.FileManagers   = fileManagers;

                        ModelState.AddModelError("CategoryId", "You should select category from category tree.");
                        return(View(content));
                    }

                    if (content.Id == 0)
                    {
                        ContentRepository.Add(content);
                        ClearCache(content.StoreId);
                    }
                    else
                    {
                        ContentRepository.Edit(content);
                    }
                    ContentRepository.Save();
                    int contentId = content.Id;
                    if (selectedFileId != null)
                    {
                        ContentFileRepository.SaveContentFiles(selectedFileId, contentId);
                    }
                    LabelLineRepository.SaveLabelLines(selectedLabelId, contentId, ContentType);

                    if (IsSuperAdmin)
                    {
                        return(RedirectToAction("Index", new { storeId = content.StoreId, categoryId = content.CategoryId }));
                    }
                    else
                    {
                        return(RedirectToAction("Index", new { categoryId = content.CategoryId }));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Unable to save changes:" + ex.StackTrace, content);
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return(View(content));
        }