protected void btnAgregar_Click(object sender, EventArgs e) { ProductsLogic productsLogic = new ProductsLogic(); productsLogic.Add(new Products { //ProductID = productsLogic.GetLastID() + 1, /* Tiene generador de ID automático, no es necesario */ ProductName = this.txtNombre.Text, QuantityPerUnit = this.txtCantidad.Text, UnitPrice = decimal.Parse(this.txtPrecio.Text) }); ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "alert('Producto agregado!'); window.location='" + Request.ApplicationPath + "Default.aspx';", true); }
public IHttpActionResult PostProducts(ProductsView productsView) { Products product = new Products { ProductName = productsView.Name, QuantityPerUnit = productsView.QuantityPerUnit, UnitPrice = productsView.Price, }; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } productsLogic.Add(product); //Para retornar en la vista el ID correcto y no 0 productsView.Id = product.ProductID; //return CreatedAtRoute("DefaultApi", new { id = product.ProductID }, productsView); return(Ok()); }
public ActionResult InsertEdit(ProductsView productsView) { if (ModelState.IsValid) { if (productsView.Id <= 0) { try { Products productEntity = new Products { ProductName = productsView.Name, QuantityPerUnit = productsView.QuantityPerUnit, UnitPrice = productsView.Price, }; logic.Add(productEntity); return(RedirectToAction("Index")); } catch (FormatException ex) { string error = $"Price error, non-numeric value was entered. Error: {ex.Message}"; TempData["MensajeError"] = error; LogErrorsLogic logErrorsLogic = new LogErrorsLogic(); logErrorsLogic.LogError(error); return(RedirectToAction("Index", "Error")); } catch (Exception ex) { LogErrorsLogic logErrorsLogic = new LogErrorsLogic(); logErrorsLogic.LogError(ex.Message); TempData["MensajeError"] = "There was a problem trying to save the product. Please go back and try again"; return(RedirectToAction("Index", "Error")); } } else { try { Products productEntity = new Products { ProductID = productsView.Id, ProductName = productsView.Name, QuantityPerUnit = productsView.QuantityPerUnit, UnitPrice = productsView.Price, }; logic.Update(productEntity); return(RedirectToAction("Index")); } catch (Exception ex) { LogErrorsLogic logErrorsLogic = new LogErrorsLogic(); logErrorsLogic.LogError(ex.Message); TempData["MensajeError"] = "There was a problem trying to save the product. Please go back and try again"; return(RedirectToAction("Index", "Error")); } } } return(View()); }