Ejemplo n.º 1
0
        public ActionResult Create(ProductoViewModel producto)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Files.Count > 0)
                    {
                        var file = Request.Files[0];

                        if (file != null && file.ContentLength > 0)
                        {
                            // Guardo la foto en un espacio fisico primero en la carpeta /Images
                            var fileName = Path.GetFileName(file.FileName);
                            var path     = Path.Combine(Server.MapPath("~/Images" + fileName));
                            file.SaveAs(path);

                            using (var binaryReader = new BinaryReader(file.InputStream))
                            {
                                byte[] array = binaryReader.ReadBytes(file.ContentLength);

                                var foto = Mapper.Map(array, producto.Imagen);
                                // Aqui mapeo la foto
                                var productoDominio = Mapper.Map <ProductoViewModel, Producto>(producto);
                                productoDominio.Imagen = foto;

                                //Se agrega la foto a la BD
                                _productoAppService.Agregar(productoDominio);
                            }



                            return(RedirectToAction("Index"));
                        }


                        //Mapeo el viewmodel "productoSinimagen" a una entidad
                        var productoSinimagen = Mapper.Map <ProductoViewModel, Producto>(producto);
                        // Actualizo la entidad
                        _productoAppService.Agregar(productoSinimagen);

                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception ex)
                {
                    return(View(ex.Message));
                }
            }


            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));
        }
Ejemplo n.º 2
0
        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);
            }
        }