public JsonResult RemoveSupplier(RemoveSupplierModel model) { RemoveSupplierJsonModel responseModel = new RemoveSupplierJsonModel(); try { ProductListViewModel partialViewModel; using (ProductListContext productContext = new ProductListContext()) { Supplier supplierToRemove = productContext.Suppliers.FirstOrDefault(s => s.SupplierID == model.SupplierID); if (supplierToRemove != null) { // Remove any products associated with the supplier foreach (ProductBase product in productContext.AllProducts.Where(p => p.SupplierID == supplierToRemove.SupplierID)) { product.Removed = true; } // Remove the supplier supplierToRemove.Removed = true; productContext.SaveChanges(); } responseModel.SuppliersRemain = productContext.Suppliers.Count(s => !s.Removed) > 0; responseModel.ProductsRemain = productContext.AllProducts.Count(p => !p.Removed) > 0; // create the model so we can render the new version of the table partialViewModel = ProductListViewModel.GetModel(productContext); } responseModel.Success = true; // render the table responseModel.Content = MvcHelper.RenderControllerPartialViewToString(this, "_SupplierListTable", partialViewModel); responseModel.AdditionalDataTables = MvcHelper.RenderControllerPartialViewToString(this, "_AdditionalDataTables", partialViewModel); } catch (Exception e) { responseModel.Success = false; responseModel.Content = Errors.GenericMVCInternalError; ErrorLog.LogError(e); } return(Json(responseModel)); }
public JsonResult RemoveProduct(RemoveProductModel model) { RemoveProductResponseModel responseModel = new RemoveProductResponseModel(); try { ProductListViewModel partialViewModel; using (ProductListContext productContext = new ProductListContext()) { ProductBase productToRemove = productContext.AllProducts.FirstOrDefault(p => p.ProductID == model.ProductID); if (productToRemove != null) { productToRemove.Removed = true; } productContext.SaveChanges(); // create the model so we can render the new version of the table partialViewModel = ProductListViewModel.GetModel(productContext); responseModel.Success = true; responseModel.ProductsRemain = productContext.AllProducts.Count(p => !p.Removed) > 0; // render the table responseModel.Content = MvcHelper.RenderControllerPartialViewToString(this, "_ProductListTable", partialViewModel); responseModel.AdditionalDataTables = MvcHelper.RenderControllerPartialViewToString(this, "_AdditionalDataTables", partialViewModel); } } catch (Exception e) { responseModel.Success = false; responseModel.Content = Errors.GenericMVCInternalError; ErrorLog.LogError(e); } return(Json(responseModel)); }