public int AddCategory(Category category) { if (_context.Entry(category).State == EntityState.Detached) { _context.Categories.Attach(category); } _context.Categories.Add(category); _context.SaveChanges(); return category.Id; }
public static MvcHtmlString CategoryLink(this HtmlHelper helper, Category category) { return helper.ActionLink( category.Name, "Category", "Blog", new {selectedCategory = category.UrlSlug}, new { title = String.Format("See all posts about {0}.", category.Name), @class = "categoryLink" }); }
public void EditCategory(Category category) { DbEntityEntry<Category> entry = _context.Entry(category); if (entry.State == EntityState.Detached) { DbSet set = _context.Set(category.GetType()); var attachedEntity = (Category) set.Find(category.Id); if (attachedEntity != null) { DbEntityEntry<Category> attachedEntry = _context.Entry(attachedEntity); attachedEntry.CurrentValues.SetValues(category); } } else { entry.State = EntityState.Modified; } _context.SaveChanges(); }
public ContentResult EditCategory(Category category) { string json; if (ModelState.IsValid){ _blogRepository.EditCategory(category); json = JsonConvert.SerializeObject(new { id = category.Id, success = true, message = "Changes saved successfully." }); } else{ json = JsonConvert.SerializeObject(new { id = 0, success = false, message = "Failed to save the changes." }); } return Content(json, "application/json"); }