public async Task<ActionResult> CategoryUpdate(CategoryModel model) { if (model.Category.ID == 0) { model.Category.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added; model.Category.Parent = 0; _categoryService.Insert(model.Category); } else { var categoryExisting = await _categoryService.FindAsync(model.Category.ID); categoryExisting.Name = model.Category.Name; categoryExisting.Description = model.Category.Description; categoryExisting.Enabled = model.Category.Enabled; categoryExisting.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Modified; _categoryService.Update(categoryExisting); } await _unitOfWorkAsync.SaveChangesAsync(); // Delete existing category item types var ListingTypes = _categoryListingTypeService.Queryable().Where(x => x.CategoryID == model.Category.ID).Select(x => x.ID).ToList(); foreach (var ListingTypeID in ListingTypes) { await _categoryListingTypeService.DeleteAsync(ListingTypeID); } // Insert category item types foreach (var categoryListingTypeID in model.CategoryListingTypeID) { _categoryListingTypeService.Insert(new CategoryListingType() { CategoryID = model.Category.ID, ListingTypeID = categoryListingTypeID, ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added }); } await _unitOfWorkAsync.SaveChangesAsync(); _dataCacheService.RemoveCachedItem(CacheKeys.Categories); return RedirectToAction("Categories"); }
public async Task<ActionResult> CategoryUpdate(int? id) { Category category; var categoryModel = new CategoryModel(); var ListingTypes = await _ListingTypeService.Query().SelectAsync(); categoryModel.ListingTypes = ListingTypes.ToList(); if (id.HasValue) { category = await _categoryService.FindAsync(id); var categoryListingTypes = await _categoryListingTypeService.Query(x => x.CategoryID == id.Value).SelectAsync(); categoryModel.CategoryListingTypeID = categoryListingTypes.Select(x => x.ListingTypeID).ToList(); } else category = new Category() { Enabled = true }; categoryModel.Category = category; return View(categoryModel); }