public IHttpActionResult EditCategory(int id, [FromBody] AdminCategoryBindingModel model) { // Validate the input parameters if (!ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } // Find the category for editing var category = this.Data.Categories.All().FirstOrDefault(d => d.Id == id); if (category == null) { return(this.BadRequest("Category #" + id + " not found!")); } // Modify the category properties category.Name = model.Name; // Save the changes in the database this.Data.Categories.SaveChanges(); return(this.Ok( new { message = "Category #" + id + " edited successfully." } )); }
public IHttpActionResult CreateNewCategory([FromBody] AdminCategoryBindingModel model) { // Validate the input parameters if (!ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } // Create new category and assign its properties form the model var category = new Category { Name = model.Name }; // Save the changes in the database this.Data.Categories.Add(category); this.Data.Categories.SaveChanges(); return(this.Ok( new { message = "Category #" + category.Id + " created." } )); }