private void btnGuardar_Click(object sender, EventArgs e) { try { if (this.txtCodigo.Text == string.Empty || this.txtCompra.Text == string.Empty || this.txtDescripcion.Text == string.Empty || this.txtStock.Text == string.Empty || this.txtTalle.Text == string.Empty || this.txtVenta.Text == string.Empty) { MensajeError("Faltan ingresar algunos datos, observe las alertas"); if (this.txtCodigo.Text == string.Empty) { ErrorProvider.SetError(txtCodigo, "Ingrese el Codigo"); } if (this.txtCompra.Text == string.Empty) { ErrorProvider.SetError(txtCompra, "Ingrese el Precio de Compra"); } if (this.txtDescripcion.Text == string.Empty) { ErrorProvider.SetError(txtDescripcion, "Ingrese el Nombre del Producto"); } if (this.txtStock.Text == string.Empty) { ErrorProvider.SetError(txtStock, "Ingrese el Stock del Producto"); } if (this.txtTalle.Text == string.Empty) { ErrorProvider.SetError(txtTalle, "Ingrese el Talle del Producto"); } if (this.txtVenta.Text == string.Empty) { ErrorProvider.SetError(txtVenta, "Ingrese el Precio de Venta"); } } else { System.IO.MemoryStream ms = new System.IO.MemoryStream(); this.picImagen.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] imagen = ms.GetBuffer(); if (this.IsNuevo) { Producto nuevo = new Producto(); nuevo.Codigo = Convert.ToInt32(this.txtCodigo.Text); nuevo.Descripcion = this.txtDescripcion.Text; nuevo.PrecioCompra = Convert.ToDouble(this.txtCompra.Text); nuevo.PrecioVenta = Convert.ToDouble(this.txtVenta.Text); nuevo.Talle = this.txtTalle.Text; nuevo.Stock = Convert.ToInt32(this.txtStock.Text); nuevo.MarcaId = Convert.ToInt32(this.comboMarca.SelectedValue); nuevo.TerceroId = Convert.ToInt32(this.combo_tercero.SelectedValue); nuevo.CategoriaId = Convert.ToInt32(this.combo_Categoria.SelectedValue); if (imagen != null) { nuevo.Imagen = imagen; _productoAppservice.Agregar(nuevo); MensajeOk("Se grabó el producto correctamente"); } else { MensajeError("Elija una imagen para el producto"); } } else if (this.IsModificar) { Producto modificar = new Producto(); modificar = _productoAppservice.BuscarporId(Convert.ToInt32(this.txtId.Text)); modificar.Codigo = Convert.ToInt32(this.txtCodigo.Text); modificar.Descripcion = this.txtDescripcion.Text; modificar.PrecioCompra = Convert.ToDouble(this.txtCompra.Text); modificar.PrecioVenta = Convert.ToDouble(this.txtVenta.Text); modificar.Talle = this.txtTalle.Text; modificar.Stock = Convert.ToInt32(this.txtStock.Text); modificar.MarcaId = Convert.ToInt32(this.comboMarca.SelectedValue); modificar.TerceroId = Convert.ToInt32(this.combo_tercero.SelectedValue); modificar.CategoriaId = Convert.ToInt32(this.combo_Categoria.SelectedValue); if (imagen != null) { modificar.Imagen = imagen; } _productoAppservice.Actualizar(modificar); MensajeOk("Se actualizó el producto correctamente"); } this.Limpiar(); this.Mostrar(); this.dgvProductos.Enabled = true; this.tabControl1.TabIndex = 1; this.Habilitar(false); } } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } }
public ActionResult Edit(ProductoViewModel producto) { if (ModelState.IsValid) { try { //Verifico si hay algun request if (Request.Files.Count > 0) { //obtengo el valor del Input del file de la vista var file = Request.Files[0]; //Si esta vacio o es igual a 0 quiere decir que no se paso una imagen if (file != null && file.ContentLength > 0) { // guardo la imagen en un la carpeta "Images" para luego llevarlo a la BD var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Images" + fileName)); file.SaveAs(path); // Realizo la conversion de datos using (var binaryReader = new BinaryReader(file.InputStream)) { byte[] array = binaryReader.ReadBytes(file.ContentLength); // mapeo la foto convertida a bytes, hacia el viewmodel var foto = Mapper.Map(array, producto.Imagen); //mapeo el viewmodel como una entidad var productoDominio = Mapper.Map <ProductoViewModel, Producto>(producto); //agrego la foto en bytes a la entidad productoDominio.Imagen = foto; //actualizo la entidad en la BD _productoAppService.Actualizar(productoDominio); } } else { // Obtengo el Id de la imagen que estoy por Actualizar var pasoid = producto.Id; // Obtengo los datos de la imagen que estoy por actualizar con el Id var pasofoto = _productoAppService.BuscarProductoImagen(pasoid); //Convierto la Entidad a un viewmodel var viewmodel = Mapper.Map <Producto, ProductoViewModel>(pasofoto); //Modifico la propiedad imagen del viewmodel al nuevo viewmodel creado producto.Imagen = viewmodel.Imagen; //Mapeo el viewmodel "producto" a una entidad var productoDominio = Mapper.Map <ProductoViewModel, Producto>(producto); // Actualizo la entidad _productoAppService.Actualizar(productoDominio); } return(RedirectToAction("Index")); } } catch (Exception ex) { return(View(ex.Message)); } return(RedirectToAction("Index")); } ViewBag.MarcaId = new SelectList(_marcaAppService.ObtenerTodo(), "Id", "Descripcion", producto.MarcaId); ViewBag.CategoriaId = new SelectList(_categoriaAppService.ObtenerTodo(), "Id", "Descripcion", producto.CategoriaId); ViewBag.TerceroId = new SelectList(_terceroAppService.ObtenerTodo(), "Id", "Apellido", producto.TerceroId); return(View(producto)); }