public ActionResult Edit([Bind(Include = "Id,Name,ThumbailUrl,Mp3Url,Brief,Content,Publish,Order")] Article article) { if (ModelState.IsValid) { var update = db.Articles.Find(article.Id); if (TryUpdateModel(update, new[] { "Id", "Name", "ThumbailUrl", "Mp3Url", "Brief", "Content", "Publish", "Order" })) { if (article.Publish) { update.PublishedDate = DateTime.Now; } db.Entry(update).State = EntityState.Modified; db.SaveChanges(); return(update.Publish ? RedirectToAction("Index"): RedirectToAction("Edit", new { id = update.Id })); } } ViewBag.ArticleParentId = new SelectList(db.Articles, "Id", "Name", article.ArticleParentId); return(View(article)); }
public ActionResult Edit(Article article, int[] catalogIds) { if (ModelState.IsValid) { var update = db.Articles.Find(article.Id); if (TryUpdateModel(update, new[] { "Id", "Name", "ThumbailUrl", "Mp3Url", "Brief", "Content", "Publish", "Order" })) { update.Catalogs.RemoveAll(c => true); if (catalogIds != null) { update.Catalogs = db.Catalogs.Where(c => catalogIds.Any(id => id == c.Id)).ToList(); } db.Entry(update).State = EntityState.Modified; db.SaveChanges(); } return(article.Publish ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = article.Id })); } ViewBag.ArticleParentId = new SelectList(db.Articles, "Id", "Name", article.ArticleParentId); return(View(article)); }