예제 #1
0
        public IActionResult Agregar(ProductosViewModel vm)
        {
            fruteriashopContext context = new fruteriashopContext();

            if (vm.Archivo.ContentType != "image/jpeg" || vm.Archivo.Length > 1024 * 1024 * 2)
            {
                ModelState.AddModelError("", "Debe selecionar un archivo jpg de menos de 2mb");
                CategoriasRepository categoriasRepository = new CategoriasRepository(context);
                vm.Categorias = categoriasRepository.GetAll();

                return(View(vm));
            }
            try
            {
                ProductosRepository repos = new ProductosRepository(context);
                repos.Insert(vm.Producto);
                //Guardar archivo de inserción
                FileStream fs = new FileStream(Environment.WebRootPath + "/img_frutas/" + vm.Producto.Id + ".jpg", FileMode.Create);
                vm.Archivo.CopyTo(fs);
                fs.Close();

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                CategoriasRepository categoriasRepository = new CategoriasRepository(context);
                vm.Categorias = categoriasRepository.GetAll();

                return(View(vm));
            }
        }
        public IActionResult Agregar(ProductosViewModel pvm)
        {
            fruteriashopContext context = new fruteriashopContext();

            //guardar el archivo de la imagen


            try
            {
                if (pvm.Archivo == null)
                {
                    ModelState.AddModelError("", "Debe seleccionar una imagen para el producto");
                    CategoriasRepository categoriasRepository = new CategoriasRepository(context);

                    pvm.Categorias = categoriasRepository.GetAll();

                    return(View(pvm));
                }
                else
                {
                    if (pvm.Archivo.ContentType != "image/jpeg" || pvm.Archivo.Length > 1024 * 1024 * 2)
                    {
                        ModelState.AddModelError("", "Debe seleccionar un archivo jpg de menos de 2MB.");
                        CategoriasRepository categoriasRepository = new CategoriasRepository(context);

                        pvm.Categorias = categoriasRepository.GetAll();

                        return(View(pvm));
                    }
                }


                ProductosRepository repos = new ProductosRepository(context);

                repos.Insert(pvm.Producto);

                if (pvm.Archivo != null)
                {
                    FileStream fs = new FileStream(Enviroment.WebRootPath + "/img_frutas/" + pvm.Producto.Id + ".jpg", FileMode.Create);
                    pvm.Archivo.CopyTo(fs);
                    fs.Close();
                }



                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                CategoriasRepository categoriasRepository = new CategoriasRepository(context);

                pvm.Categorias = categoriasRepository.GetAll();

                return(View(pvm));
            }
        }
예제 #3
0
        public IActionResult Agregar(ProductosViewModel vm)
        {
            fruteriashopContext context = new fruteriashopContext();

            try
            {
                if (vm.Archivo == null)
                {
                    ModelState.AddModelError("", "Debe seleccionar la imagen del producto.");
                    CategoriasRepository categoriasrepos = new CategoriasRepository(context);
                    vm.Categorias = categoriasrepos.GetAll().Where(x => x.Eliminado == false).OrderBy(x => x.Nombre);
                    return(View(vm));
                }
                else
                {
                    if (vm.Archivo.ContentType != "image/jpeg" || vm.Archivo.Length > 1024 * 1024 * 2)
                    {
                        ModelState.AddModelError("", "Debe seleccionar un archivo jpg de menos de 2MB.");
                        CategoriasRepository repos = new CategoriasRepository(context);
                        vm.Categorias = repos.GetAll().Where(x => x.Eliminado == false).OrderBy(x => x.Nombre);
                        return(View(vm));
                    }
                }


                ProductosRepository productosrepos = new ProductosRepository(context);
                productosrepos.Insert(vm.Producto);

                if (vm.Archivo != null)
                {
                    FileStream fs = new FileStream(Enviroment.WebRootPath + "/img_frutas/" + vm.Producto.Id + ".jpg", FileMode.Create);
                    vm.Archivo.CopyTo(fs);
                    fs.Close();
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                CategoriasRepository repos = new CategoriasRepository(context);
                vm.Categorias = repos.GetAll().OrderBy(x => x.Nombre);
                return(View(vm));
            }
        }