// GET: Products/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = db.Products.Find(id); if (product == null) { return(HttpNotFound()); } var PModel = new ProductCreateEditViewModel(); PModel.ShortDescriptions = db.ShortDescriptions.ToList(); PModel.FullDescriptions = db.FullDescriptions.ToList(); PModel.Observations = db.Observations.ToList(); PModel.Recommendations = db.Recommendations.ToList(); PModel.Capacities = db.Capacities.ToList(); PModel.Categories = db.Categories.ToList(); PModel.Colors = db.Colors.ToList(); PModel.Lines = db.Lines.ToList(); PModel.Origins = db.Origins.ToList(); PModel.Parts = db.Parts.ToList(); PModel.Product = product; return(View(PModel)); }
public async Task <IActionResult> Edit(int?id, string FarmerId) { if (id == null & FarmerId != UserId()) { return(NotFound()); } ProductCreateEditViewModel model; var product = await _context.Products .Where(i => i.FarmerId == UserId()) .FirstOrDefaultAsync(m => m.ID == id); if (product == null) { return(NotFound()); } model = new ProductCreateEditViewModel { ID = product.ID, Title = product.Title, Description = product.Description, IsActive = product.IsActive, Amount = product.Amount, Price = product.Price }; ViewBag.Image = product.Image; return(View(model)); }
// GET: Products/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = _purchaseUow.Products.GetById(id); if (product == null) { return(HttpNotFound()); } var vm = new ProductCreateEditViewModel() { Product = product, ProductTypeSelectList = new SelectList(_purchaseUow.ProductTypes.All, "ProductTypeId", "Description", product.ProductTypeId), WorkMultiSelectList = new MultiSelectList(_warehouseUow.WorkTypes.All, "WorkTypeId", "Description", _warehouseUow.ProductInWorks.All.Where(a => a.ProductId == product.ProductId).Select(b => b.WorkTypeId).ToArray()), WarehouseMultiSelectList = new MultiSelectList(_warehouseUow.Warehouses.All, "WarehouseId", "Description", _warehouseUow.ProductInWarehouses.All.Where(a => a.ProductId == product.ProductId).Select(b => b.WarehouseId).ToArray()) }; return(View(vm)); }
public async Task <IActionResult> Create([Bind("ID,Title,Description,IsActive,Amount,Image,Price,FarmerId")] ProductCreateEditViewModel model) { if (ModelState.IsValid) { var product = new Product { FarmerId = UserId(), CreatedDate = DateTime.Now, Title = model.Title, Description = model.Description, IsActive = model.IsActive, Amount = model.Amount, Price = model.Price }; if (model.Image != null && model.Image.Length > 0) { using (var memoryStream = new MemoryStream()) { await model.Image.CopyToAsync(memoryStream); product.Image = memoryStream.ToArray(); } } _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details), new { id = product.ID })); } return(View(model)); }
// GET: Products/Create public ActionResult Create() { var vm = new ProductCreateEditViewModel() { ProductTypeSelectList = new SelectList(_purchaseUow.ProductTypes.All, nameof(ProductType.ProductTypeDescriptionId), nameof(ProductType.ProductTypeDescription)) }; var x = vm; return(View(vm)); }
public async Task <IActionResult> Edit(int id, [Bind("ID,Title,Description,IsActive,Amount,Image,Price,FarmerId")] ProductCreateEditViewModel model) { if (id != model.ID) { return(NotFound()); } if (ModelState.IsValid) { var product = await _context.Products .Where(i => i.FarmerId == UserId()) .FirstOrDefaultAsync(m => m.ID == id); try { product.FarmerId = UserId(); product.ID = model.ID; product.Title = model.Title; product.Description = model.Description; product.IsActive = model.IsActive; product.Amount = model.Amount; product.Price = model.Price; if (model.Image != null && model.Image.Length > 0) { using (var memoryStream = new MemoryStream()) { await model.Image.CopyToAsync(memoryStream); product.Image = memoryStream.ToArray(); } } _context.Update(product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(model.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), new { id = product.ID })); } return(View(model)); }
// GET: Products/Create public ActionResult Create() { var PModel = new ProductCreateEditViewModel(); using (var db = new TPW()) { PModel.ShortDescriptions = db.ShortDescriptions.ToList(); PModel.FullDescriptions = db.FullDescriptions.ToList(); PModel.Observations = db.Observations.ToList(); PModel.Recommendations = db.Recommendations.ToList(); PModel.Capacities = db.Capacities.ToList(); PModel.Categories = db.Categories.ToList(); PModel.Colors = db.Colors.ToList(); PModel.Lines = db.Lines.ToList(); PModel.Origins = db.Origins.ToList(); PModel.Parts = db.Parts.ToList(); //Products = db.Products.ToList(); } return(View(PModel)); }
public ActionResult Create(ProductCreateEditViewModel vm) { if (ModelState.IsValid) { foreach (var workId in vm.WorkIds) { vm.Product.ProductInWorks.Add(new ProductInWork() { WorkTypeId = workId //ProductId = vm.Product.ProductId }); } _purchaseUow.Products.Add(vm.Product); _purchaseUow.Commit(); return(RedirectToAction("Index")); } vm.WorkMultiSelectList = new MultiSelectList(_warehouseUow.WorkTypes.All, "WorkTypeId", "Description"); vm.ProductTypeSelectList = new SelectList(_purchaseUow.ProductTypes.All.Select(a => a.ProductTypeDescription.Translations.Where(b => b.Culture == CultureHelper.GetCurrentNeutralUICulture())), "ProductTypeId", "Description"); vm.WarehouseMultiSelectList = new MultiSelectList(_warehouseUow.Warehouses.All, "WarehouseId", "Description"); return(View(vm)); }
public ActionResult Edit(ProductCreateEditViewModel vm) { if (ModelState.IsValid) { foreach (var productInWorks in _warehouseUow.ProductInWorks.All.Where(a => a.ProductId == vm.Product.ProductId)) { _warehouseUow.ProductInWorks.Delete(productInWorks); } foreach (var productInWarehouses in _warehouseUow.ProductInWarehouses.All.Where(a => a.ProductId == vm.Product.ProductId)) { _warehouseUow.ProductInWarehouses.Delete(productInWarehouses); } _warehouseUow.Products.Update(vm.Product); _purchaseUow.Commit(); foreach (var workId in vm.WorkIds) { vm.Product.ProductInWorks.Add(new ProductInWork() { WorkTypeId = workId, ProductId = vm.Product.ProductId }); } foreach (var warehouseId in vm.WarehouseIds) { vm.Product.ProductInWarehouses.Add(new ProductInWarehouse() { WarehouseId = warehouseId, ProductId = vm.Product.ProductId }); } _purchaseUow.Commit(); return(RedirectToAction("Index")); } vm.ProductTypeSelectList = new SelectList(_purchaseUow.ProductTypes.All, "ProductTypeId", "Description"); vm.WorkMultiSelectList = new MultiSelectList(_warehouseUow.WorkTypes.All, "WorkTypeId", "Description"); return(View(vm)); }