public JsonResult guardar(cProductosInventario cproductosInventario)
        {
            ProductosInventario objProductosInventario = new ProductosInventario();

            if (cproductosInventario.Id != 0)
            {
                //editar
                objProductosInventario = db.ProductosInventario.Where(a => a.Id == cproductosInventario.Id).FirstOrDefault();
                if (objProductosInventario == null)
                {
                    return(Json(new { status = false, mensaje = "No existe el registro" }));
                }
                else
                {
                    objProductosInventario.Codigo       = cproductosInventario.Codigo;
                    objProductosInventario.Presentacion = cproductosInventario.Presentacion;
                    objProductosInventario.Nombre       = cproductosInventario.Nombre;
                    objProductosInventario.Descripcion  = cproductosInventario.Descripcion;
                    objProductosInventario.Costo        = cproductosInventario.Costo;
                    objProductosInventario.Utilidad     = cproductosInventario.Utilidad;
                    objProductosInventario.PrecioVenta  = cproductosInventario.PrecioVenta;
                    objProductosInventario.StockInicial = cproductosInventario.StockInicial;
                    objProductosInventario.Estado       = cproductosInventario.Estado;
                    //objProductosInventario.Imagen = cproductosInventario.Imagen;
                    objProductosInventario.Id_Categoria = cproductosInventario.Id_Categoria;

                    db.ProductosInventario.Attach(objProductosInventario);
                    db.Entry(objProductosInventario).State = System.Data.Entity.EntityState.Modified;
                }
            }
            else
            {
                //nuevo
                objProductosInventario.Codigo       = cproductosInventario.Codigo;
                objProductosInventario.Presentacion = cproductosInventario.Presentacion;
                objProductosInventario.Nombre       = cproductosInventario.Nombre;
                objProductosInventario.Descripcion  = cproductosInventario.Descripcion;
                objProductosInventario.Costo        = cproductosInventario.Costo;
                objProductosInventario.Utilidad     = cproductosInventario.Utilidad;
                objProductosInventario.PrecioVenta  = cproductosInventario.PrecioVenta;
                objProductosInventario.StockInicial = cproductosInventario.StockInicial;
                objProductosInventario.Estado       = cproductosInventario.Estado;
                //objProductosInventario.Imagen = cproductosInventario.Imagen;
                objProductosInventario.Id_Categoria = cproductosInventario.Id_Categoria;
                db.ProductosInventario.Add(objProductosInventario);
            }
            db.SaveChanges();
            return(Json(new { status = true, mensaje = "Datos guardados", datos = objProductosInventario }));
        }
        public JsonResult editar(int Id)
        {
            ProductosInventario objProdInv = new ProductosInventario();

            if (Id == 0)
            {
                return(Json(new { status = false, mensaje = "El id esta en 0" }));
            }
            objProdInv = db.ProductosInventario.Where(a => a.Id == Id).FirstOrDefault();
            if (objProdInv == null)
            {
                return(Json(new { status = false, mensaje = "No existe producto en la BD" }));
            }
            return(Json(new { status = true, mensaje = "Datos cargados", datos = objProdInv }));
        }
        public JsonResult eliminar(int Id)
        {
            ProductosInventario objProdInv = new ProductosInventario();

            if (Id == 0)
            {
                return(Json(new { status = false, mensaje = "El id esta en 0" }));
            }
            objProdInv = db.ProductosInventario.Where(a => a.Id == Id).FirstOrDefault();
            if (objProdInv == null)
            {
                return(Json(new { status = false, mensaje = "No existe producto en la BD" }));
            }
            else
            {
                db.ProductosInventario.Attach(objProdInv);
                db.ProductosInventario.Remove(objProdInv);
                db.SaveChanges();

                return(Json(new { status = true, mensaje = "Registro eliminado" }));
            }
        }
        public JsonResult ImageUpload(cProductosInventario model)
        {
            //int imgId = model.Id;
            int imgId = 13;
            var file  = model.ImageFile;

            byte[] ImageByte = null;
            if (file != null)
            {
                file.SaveAs(Server.MapPath("/UploadImage/" + file.FileName));
                BinaryReader reader = new BinaryReader(file.InputStream);
                ImageByte = reader.ReadBytes(file.ContentLength);
                ProductosInventario img = new ProductosInventario();
                img        = db.ProductosInventario.Where(a => a.Id == imgId).FirstOrDefault();
                img.Imagen = ImageByte;
                db.ProductosInventario.Attach(img);
                db.Entry(img).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                //imgId = img.Id;
            }
            //return Json(imgId, JsonRequestBehavior.AllowGet);
            return(Json(new { status = true, mensaje = "Imagen guardada", JsonRequestBehavior.AllowGet }));
        }