Example #1
0
        //Create Category
        public IHttpActionResult CreateCategory([FromBody] CategoryBindindModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var newCategory = new Category()
            {
                Name = model.Name
            };

            _db.Categories.Add(newCategory);
            _db.SaveChanges();
            //convert to view model to return
            var viewModel = new CategoryViewModel()
            {
                Id   = newCategory.Id,
                Name = newCategory.Name
            };

            return(Content(HttpStatusCode.Created, viewModel));
        }
Example #2
0
        public IHttpActionResult UpdateCategory(int id, CategoryBindindModel model)
        {
            var existedCategory = _db.Categories.Find(id);

            if (existedCategory == null)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //update
            existedCategory.Name = model.Name;
            _db.SaveChanges();
            //comvert to view model to return
            var categoryViewModel = new CategoryViewModel()
            {
                Id   = existedCategory.Id,
                Name = existedCategory.Name
            };

            return(Json(categoryViewModel));
        }