public ActionResult Create(Product product) { if (!this.ModelState.IsValid) { return(View(product)); } prdClient.AddProduct(product.Name, product.Description, product.Price, product.CategoryId); return(RedirectToAction("Index")); }
private void buttonAdd_Click(object sender, EventArgs e) { int added = 0; foreach (Product p in getDataFromBindingsSource()) { if (Client.AddProduct(p) > 0) { added++; } } labelInfo.Text = "Added " + added + " product(s)"; }
private void buttonAdd_Click(object sender, EventArgs e) { ProductServiceClient product = new ProductServiceClient(); Product productToChange = new Product(); using (AddEdit edit = new AddEdit(productToChange, false)) { edit.ShowDialog(); product.AddProduct(edit.Product); products = product.GetAllProducts().ToList(); dataGridViewProducts.DataSource = products; dataGridViewProducts.Refresh(); UpdateLables(); } }
public ActionResult Edit(ProductViewModel productViewModel) { if (ModelState.IsValid) { if (productViewModel.Id <= 0) { //Create var product = Mapper.Map <Product>(productViewModel); var file = productViewModel.FileImage; if (file != null) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/Images/Products"), fileName); file.SaveAs(path); var imageUrl = Url.Action("Index", "Home"); imageUrl += "Content/Images/Products/" + fileName; productViewModel.ImageUrl = imageUrl; product.ImageUrl = imageUrl; } } else { var imageUrl = Url.Action("Index", "Home"); imageUrl += "Content/Images/Products/default.jpg"; product.ImageUrl = imageUrl; } var productCreatedId = productServiceClient.AddProduct(product); var createdProduct = productServiceClient.GetProductById(productCreatedId); var createdProductViewModel = Mapper.Map <ProductViewModel>(createdProduct); return(View(createdProductViewModel)); } else { //Update var product = Mapper.Map <Product>(productViewModel); var file = productViewModel.FileImage; if (file != null) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/Images/Products"), fileName); file.SaveAs(path); var imageUrl = Url.Action("Index", "Home"); imageUrl += "Content/Images/Products/" + fileName; productViewModel.ImageUrl = imageUrl; product.ImageUrl = imageUrl; } } else { var imageUrl = Url.Action("Index", "Home"); imageUrl += "Content/Images/Products/default.jpg"; product.ImageUrl = imageUrl; } var updatedProductId = productServiceClient.UpdateProduct(product); var updatedProduct = productServiceClient.GetProductById(updatedProductId); var updatedProductViewModel = Mapper.Map <ProductViewModel>(updatedProduct); return(View(updatedProductViewModel)); } } return(RedirectToAction("Index", "Home")); }