public async Task <IActionResult> Create(ProductCreateAndEditViewModel viewModel) { ModelState.Remove("Product.UserId"); ModelState.Remove("Product.User"); if (ModelState.IsValid) { if (hasSpecialChar(viewModel.Product.Title) || hasSpecialChar(viewModel.Product.Description)) { TempData["notice"] = "Product title and description cannot contain special characters (!@#$%^()&*)."; viewModel.ProductTypes = await _context.ProductType.ToListAsync(); return(View(viewModel)); } if (viewModel.Product.Price > 10000) { TempData["maxPrice"] = "Price cannot exceed $10,000."; viewModel.ProductTypes = await _context.ProductType.ToListAsync(); return(View(viewModel)); } var user = await GetCurrentUserAsync(); viewModel.Product.User = user; viewModel.Product.UserId = user.Id; viewModel.Product.DateCreated = DateTime.Now; _context.Add(viewModel.Product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ToSellIndex))); } return(View(viewModel)); }
// GET: Products/Create public async Task <IActionResult> Create() { var viewModel = new ProductCreateAndEditViewModel() { ProductTypes = await _context.ProductType.ToListAsync() }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, ProductCreateAndEditViewModel viewModel) { if (id != viewModel.Product.ProductId) { return(NotFound()); } ModelState.Remove("Product.UserId"); ModelState.Remove("Product.User"); if (ModelState.IsValid) { try { if (hasSpecialChar(viewModel.Product.Title) || hasSpecialChar(viewModel.Product.Description)) { TempData["notice"] = "Product title and description cannot contain special characters (!@#$%^()&*)."; viewModel.ProductTypes = await _context.ProductType.ToListAsync(); return(View(viewModel)); } if (viewModel.Product.Price > 10000) { TempData["maxPrice"] = "Price cannot exceed $10,000."; viewModel.ProductTypes = await _context.ProductType.ToListAsync(); return(View(viewModel)); } var user = await GetCurrentUserAsync(); viewModel.Product.User = user; viewModel.Product.UserId = user.Id; _context.Update(viewModel.Product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(viewModel.Product.ProductId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(ToSellIndex))); } return(View(viewModel)); }
// GET: Products/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var viewModel = new ProductCreateAndEditViewModel() { Product = await _context.Product.FindAsync(id), ProductTypes = await _context.ProductType.ToListAsync() }; if (viewModel.Product == null) { return(NotFound()); } return(View(viewModel)); }