public async Task <IActionResult> Edit(int id, CategoriesVm categories) { if (id != categories.CategoryId) { return(NotFound()); } if (ModelState.IsValid) { try { Categories model = _mapper.Map <Categories>(categories); _context.UpdateCategory(model); _context.Commit(); } catch (DbUpdateConcurrencyException) { if (!CategoriesExists(categories.CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ParentId"] = new SelectList(_context.GetCategoryList(), "CategoryId", "Title", categories.ParentId); return(View(categories)); }
public async Task <IHttpActionResult> PostCategories(CategoriesVm vm) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = Task.Factory.StartNew(() => { IUnitOfWork uow = new UnitOfWorkImp(new IRepositoryConnection[] { CategoriesRepository }); var bo = (BOCategories)vm.BOCategories(CategoriesRepository); uow.Create(bo); string err; if (!uow.Commit(out err)) { var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(err) }; throw new HttpResponseException(resp); } vm = new CategoriesVm(bo); return(true); }); await result; return(CreatedAtRoute("DefaultApi", new { id = vm.CategoryID }, vm)); }
public async Task <IActionResult> Create(CategoriesVm categories) { if (ModelState.IsValid) { Categories model = _mapper.Map <Categories>(categories); _context.AddCategory(model); _context.Commit(); AddMessage("success", "Added successfully"); return(RedirectToAction(nameof(Index))); } ViewData["ParentId"] = new SelectList(_context.GetCategoryList(), "CategoryId", "Title", categories.ParentId); return(View(categories)); }
public void AddCategory(CategoriesVm vm) { if (!string.IsNullOrEmpty(vm.NewCategory.Name)) { Category newCategory = new Category() { Id = Guid.NewGuid(), Name = vm.NewCategory.Name, Type = vm.NewCategory.Type }; newCategory.ParrentCategory = vm.ParrentCategory.SelectedItem == null ? null : _rep.Categories.GetById(Guid.Parse(vm.ParrentCategory.SelectedItem)); _rep.Categories.Insert(newCategory); _rep.Categories.Save(); } }
public CategoriesVm GetCategoryViewModel() { CategoriesVm vm = new CategoriesVm() { NewCategory = new Category(), Categories = _rep.Categories.GetAll(), }; vm.ParrentCategory = new BootstrapSelectVm() { SourceList = vm.Categories.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }), SelectedItem = null }; return(vm); }
public async Task <IHttpActionResult> PutCategories(Int32 id, CategoriesVm vm) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != vm.CategoryID) { return(BadRequest()); } var result = Task.Factory.StartNew(() => { IUnitOfWork uow = new UnitOfWorkImp(new IRepositoryConnection[] { CategoriesRepository }); var bo = (BOCategories)vm.BOCategories(CategoriesRepository); uow.Update(bo); string err; if (!uow.Commit(out err)) { var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(err) }; throw new HttpResponseException(resp); } return(true); }); await result; if (!result.Result) { return(NotFound()); } return(StatusCode(HttpStatusCode.NoContent)); }