private ViewResult getProduct(int id, string operation) { var product = new OfficeProductViewModel(); this.load(product, operation, id); return(View("Product", product)); }
public IActionResult Delete(OfficeProductViewModel vm) { this.Data.Products.Delete(vm.Product); this.Data.Save(); TempData["message"] = $"{vm.Product.Name} removed from Products."; return(RedirectToAction("Search")); }
public IActionResult Edit(OfficeProductViewModel vm) { if (ModelState.IsValid) { this.Data.Products.Update(vm.Product); this.Data.Save(); TempData["message"] = $"{vm.Product.Name} updated."; return(RedirectToAction("Search")); } this.load(vm, "Edit"); return(View("Product", vm)); }
private void load(OfficeProductViewModel vm, string op, int?id = null) { if (Operation.IsAdd(op)) { vm.Product = new OfficeProduct(); } else { vm.Product = this.Data.Products.Get(new QueryOptions <OfficeProduct> { Include = "Type", Where = op => op.OfficeProductId == (id ?? vm.Product.OfficeProductId) }); } vm.Types = this.Data.Types.List(new QueryOptions <ProductType> { OrderBy = pt => pt.Name }); }
public IActionResult Add(OfficeProductViewModel vm) { if (ModelState.IsValid) { if (Request.Form.Files.Count == 0) { this.load(vm, "Add"); ModelState.AddModelError("", "Please select an image."); return(View("Product", vm)); } foreach (var file in Request.Form.Files) { var img = new Image(); img.Title = file.FileName; var ms = new MemoryStream(); file.CopyTo(ms); img.Data = ms.ToArray(); ms.Close(); ms.Dispose(); this.Data.Images.Insert(img); this.Data.Save(); vm.Product.ImageId = img.ImageId; } this.Data.Products.Insert(vm.Product); this.Data.Save(); TempData["message"] = $"{vm.Product.Name} added to Products."; return(RedirectToAction("Index")); } this.load(vm, "Add"); return(View("Product", vm)); }