public ActionResult Create(Article model, FormCollection form) { if (ModelState.IsValid) { try { model.Tags = new List<Tag>(); var tagNames = System.Web.Helpers.Json.Decode<IList<string>>(form["TagsJson"]); var siteTags = BlogDataContext.Tags.WithSiteId(CurrentSiteId); foreach (var name in tagNames) { var tag = siteTags.FirstOrDefault(i => i.Name == name); if (tag == null) { model.Tags.Add(new Tag { Name = name, SiteId = CurrentSiteId }); } else { model.Tags.Add(tag); } } model.CreationTime = DateTime.Now; model.UpdateTime = DateTime.Now; BlogDataContext.Articles.Add(model); SaveChanges(); CalculateCount(model); ShowSuccess(SusuCMS.MessageResource.CreateSuccess); return RedirectToIndex(); } catch { ShowError(SusuCMS.MessageResource.CreateFailed); } } return View(model); }
public ActionResult Delete(int id, Article article) { try { BlogDataContext.Delete(article); SaveChanges(); CalculateCount(article); ShowSuccess(SusuCMS.MessageResource.DeleteSuccess); return RedirectToIndex(); } catch { ShowError(SusuCMS.MessageResource.DeleteFailed); } return RedirectToIndex(); }
private void CalculateCount(Article article) { if (article.Category != null) { article.Category.ArticleCount = BlogDataContext.Articles.WithSiteId(CurrentSiteId) .Count(i => i.IsOnline && i.CategoryId == article.CategoryId); } foreach (var item in article.Tags) { item.ArticleCount = BlogDataContext.Articles.WithSiteId(CurrentSiteId) .Count(i => i.Tags.Any(j => j.Id == item.Id) && i.IsOnline); } SaveChanges(); }