public async Task <ActionResult> Update(long id) { //ViewBag.Categories = await _repository.GetRepository<Category>().GetAllAsync(o => o.CategoryId == null); ViewBag.CategoryTypes = await _repository.GetRepository <CategoryType>().GetAllAsync(); Article article = await _repository.GetRepository <Article>().ReadAsync(id); //Nếu bài viết có trạng thái KHÁC đang biên tập thì không cho sửa if (article.Status != 1) { TempData["Error"] = "Bài viết không cho phép sửa!"; return(RedirectToAction("Index")); } ArticleUpdatingViewModel model = new ArticleUpdatingViewModel() { Id = article.Id, Title = article.Title, Description = article.Description, Content = article.Content, ImageDescription = article.ImageDescription, EventStartDate = article.EventStartDate.HasValue ? article.EventStartDate.Value.ToString("dd/MM/yyyy") : "", EventFinishDate = article.EventFinishDate.HasValue ? article.EventFinishDate.Value.ToString("dd/MM/yyyy") : "" }; List <long> articleCategoryIds = new List <long>(); var articleCategory = await _repository.GetRepository <ArticleCategory>().GetAllAsync(o => o.ArticleId == id); if (articleCategory != null && articleCategory.Any()) { articleCategoryIds = articleCategory.Select(o => o.CategoryId).ToList(); } ViewBag.articleCategoryIds = articleCategoryIds; return(View(model)); }
public async Task <ActionResult> Update(long id, ArticleUpdatingViewModel model) { if (ModelState.IsValid) { Article article = await _repository.GetRepository <Article>().ReadAsync(id); article.Title = StringHelper.KillChars(model.Title); article.Description = StringHelper.KillChars(model.Description); article.Content = StringHelper.KillChars(model.Content); article.ImageDescription = StringHelper.KillChars(model.ImageDescription); article.UpdateDate = DateTime.Now; article.UpdateBy = AccountId; if (!string.IsNullOrEmpty(model.EventStartDate)) { try { DateTime date = DateTime.ParseExact(model.EventStartDate, "dd/MM/yyyy", null); article.EventStartDate = date; } catch { article.EventStartDate = null; } } if (!string.IsNullOrEmpty(model.EventFinishDate)) { try { DateTime date = DateTime.ParseExact(model.EventFinishDate, "dd/MM/yyyy", null); article.EventFinishDate = date; } catch { article.EventFinishDate = null; } } int result = await _repository.GetRepository <Article>().UpdateAsync(article, AccountId); if (result > 0) { //Cập nhật danh mục cho bài viết List <long> articleCategoryIds = new List <long>(); //Danh sách danh mục hiện tại của bài viết var articleCategory1 = await _repository.GetRepository <ArticleCategory>().GetAllAsync(o => o.ArticleId == id); //articleCategory1 = article.ArticleCategories;//Chưa thử lại cách này //Id Danh sách danh mục hiện tại của bài viết if (articleCategory1 != null && articleCategory1.Any()) { articleCategoryIds = articleCategory1.Select(o => o.CategoryId).ToList(); } //Nếu người dùng chọn danh mục nào đó (hoặc danh mục đã được chọn từ lúc biên tập) if (model.CategoryId != null && model.CategoryId.Any()) { var articleCategory = model.CategoryId.Select(o => new ArticleCategory() { ArticleId = article.Id, CategoryId = Convert.ToInt64(o.ToString()) }); //Danh sách danh mục hiện tại không chứa danh mục được chọn thì xóa var articleCategoryDelete = articleCategory1.Where(o => !articleCategory.Any(x => x.CategoryId == o.CategoryId)); try { int result2 = await _repository.GetRepository <ArticleCategory>().DeleteAsync(articleCategoryDelete, AccountId); } catch { } //Danh sách danh mục được chọn không có trong danh mục hiện tại thì thêm var articleCategoryAdd = articleCategory.Where(o => !articleCategoryIds.Contains(o.CategoryId)); try { int result2 = await _repository.GetRepository <ArticleCategory>().CreateAsync(articleCategoryAdd, AccountId); } catch { } } else {//Nếu người dùng không chọn vào danh mục nào thì xóa tất cái hiện tại int result3 = await _repository.GetRepository <ArticleCategory>().DeleteAsync(articleCategory1, AccountId); } TempData["Success"] = "Cập nhật bài viết thành công!"; return(RedirectToAction("Index")); } else { //ViewBag.Categories = await _repository.GetRepository<Category>().GetAllAsync(o => o.CategoryId == null); ViewBag.CategoryTypes = await _repository.GetRepository <CategoryType>().GetAllAsync(); List <long> articleCategoryIds = new List <long>(); var articleCategory = await _repository.GetRepository <ArticleCategory>().GetAllAsync(o => o.ArticleId == id); if (articleCategory != null && articleCategory.Any()) { articleCategoryIds = articleCategory.Select(o => o.CategoryId).ToList(); } ViewBag.listArticleCategory = articleCategoryIds; ModelState.AddModelError(string.Empty, "Cập nhật bài viết không thành công! Vui lòng kiểm tra và thử lại!"); return(View(model)); } } else { //ViewBag.Categories = await _repository.GetRepository<Category>().GetAllAsync(o => o.CategoryId == null); ViewBag.CategoryTypes = await _repository.GetRepository <CategoryType>().GetAllAsync(); List <long> articleCategoryIds = new List <long>(); var articleCategory = await _repository.GetRepository <ArticleCategory>().GetAllAsync(o => o.ArticleId == id); if (articleCategory != null && articleCategory.Any()) { articleCategoryIds = articleCategory.Select(o => o.CategoryId).ToList(); } ViewBag.articleCategoryIds = articleCategoryIds; ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin!"); return(View(model)); } }