public ContentResult AddCategory(Category category) { string json; //variable for passing successful or unsuccessful json data ModelState.Clear(); //remove items from the model state dictionary // if the current category is valid if (TryValidateModel(category)) { var id = blogRepository.AddCategory(category); // get id from newly add category and attempt to save data // if id equals the new id, category addition was successful json = JsonConvert.SerializeObject(new { id = id, success = true, message = "Category added succesfully." }); } // else if id = 0; adding the category failed else { json = JsonConvert.SerializeObject(new { id = 0, success = false, message = "Failed to add the category." }); } return Content(json, "application/json"); // send results to the user for display }
// returns the Id of the added category public int AddCategory(Category category) { context.Categories.Add(category); // adds category to queue context.SaveChanges(); // saves changes in queue. return category.CategoryId; }
// method that saves the edited changes in a category public void EditCategory(Category category) { context.Categories.Attach(category); // add category to queue context.Entry(category).State = EntityState.Modified; // makes category's state set to modified context.SaveChanges(); // save changes in queue }