示例#1
0
 public ActionResult Edit(NewsManageModel model, SubmitTypeEnums submit)
 {
     if (ModelState.IsValid)
     {
         var response = _newsServices.SaveNewsManageModel(model);
         if (response.Success)
         {
             SetSuccessMessage(response.Message);
             switch (submit)
             {
                 case SubmitTypeEnums.Save:
                     return RedirectToAction("Index");
                 default:
                     return RedirectToAction("Edit", new { id = model.Id });
             }
         }
         SetErrorMessage(response.Message);
     }
     model.StatusList = _newsServices.GetStatus();
     model.NewsCategories = _newsCategoryServices.GetNewsCategories(model.Id);
     return View(model);
 }
示例#2
0
        /// <summary>
        /// Save news manage model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel SaveNewsManageModel(NewsManageModel model)
        {
            ResponseModel response;
            var news = GetById(model.Id);

            #region Edit News
            if (news != null)
            {
                news.Title = model.Title;
                news.Status = model.Status;
                news.Description = model.Description;
                news.Content = model.Content;
                news.ImageUrl = model.ImageUrl;
                news.IsHotNews = model.IsHotNews;

                var currentCategories = news.NewsNewsCategories.Select(nc => nc.NewsCategoryId).ToList();
                foreach (var id in currentCategories)
                {
                    if (!model.NewsCategoryIds.Contains(id))
                    {
                        _newsNewsCategoryRepository.Delete(id);
                    }
                }
                foreach (var categoryId in model.NewsCategoryIds)
                {
                    if (currentCategories.All(n => n != categoryId))
                    {
                        var newsNewsCategory = new NewsNewsCategory
                        {
                            NewsId = news.Id,
                            NewsCategoryId = categoryId
                        };
                        _newsNewsCategoryRepository.Insert(newsNewsCategory);
                    }
                }

                //Get page record order
                response = Update(news);
                return response.SetMessage(response.Success ?
                    _localizedResourceServices.T("AdminModule:::News:::Messages:::UpdateSuccessfully:::Update news successfully.")
                    : _localizedResourceServices.T("AdminModule:::News:::Messages:::UpdateFailure:::Update news failed. Please try again later."));
            }
            #endregion

            news = new EntityModel.News
            {
                Title = model.Title,
                Status = model.Status,
                Description = model.Description,
                Content = model.Content,
                ImageUrl = model.ImageUrl,
                IsHotNews = model.IsHotNews,
                Updated = DateTime.Now,
                UpdatedBy = WorkContext.CurrentUser.Email
            };

            response = Insert(news);
            foreach (var categoryId in model.NewsCategoryIds)
            {
                var newsNewsCategory = new NewsNewsCategory
                {
                    NewsId = news.Id,
                    NewsCategoryId = categoryId
                };
                _newsNewsCategoryRepository.Insert(newsNewsCategory);
            }
            return response.SetMessage(response.Success ?
                _localizedResourceServices.T("AdminModule:::News:::Messages:::CreateSuccessfully:::Create news successfully.")
                : _localizedResourceServices.T("AdminModule:::News:::Messages:::CreateFailure:::Create news failed. Please try again later."));
        }