示例#1
0
        public ActionResult Create(MarcaEditViewModel marcaVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(marcaVm));
            }

            MarcaEditDto marcaDto = mapper.Map <MarcaEditDto>(marcaVm);

            if (servicio.Existe(marcaDto))
            {
                ModelState.AddModelError(string.Empty, "Registro existente...");
                return(View(marcaVm));
            }

            try
            {
                servicio.Guardar(marcaDto);
                TempData["Msg"] = "Registro agregado";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
                return(View(marcaVm));
            }
        }
示例#2
0
 private Marca ConstruirMarca(MarcaEditViewModel marcaVm)
 {
     return(new Marca
     {
         MarcaId = marcaVm.MarcaId,
         NombreMarca = marcaVm.NombreMarca,
         Logo = marcaVm.Logo
     });
 }
示例#3
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            MarcaEditDto       marcaDto = servicio.GetMarcaId(id);
            MarcaEditViewModel marcaVm  = mapper.Map <MarcaEditViewModel>(marcaDto);

            return(View(marcaVm));
        }
示例#4
0
        public ActionResult Create(MarcaEditViewModel marcaVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(marcaVm));
            }

            Marca marca = ConstruirMarca(marcaVm);

            if (_dbContext.Marcas.Any(m => m.NombreMarca == marca.NombreMarca))
            {
                ModelState.AddModelError(string.Empty, "Registro repetido!!!");
                return(View(marcaVm));
            }

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    var    folder = "~/Content/Images/Marcas/";
                    string file   = "";
                    _dbContext.Marcas.Add(marca);
                    _dbContext.SaveChanges();
                    if (marcaVm.LogoFile != null)
                    {
                        file = $"{marca.MarcaId}.jpg";
                        var response = Helper.UploadPhoto(marcaVm.LogoFile, folder, file);
                        if (!response)
                        {
                            file = "SinImagenDisponible.jpg";
                        }
                    }
                    else
                    {
                        file = "SinImagenDisponible.jpg";
                    }
                    marca.Logo = $"{folder}{file}";
                    _dbContext.Entry(marca).State = EntityState.Modified;
                    _dbContext.SaveChanges();
                    transaction.Commit();
                    TempData["Msg"] = "Registro agregado";

                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                    return(View(marcaVm));
                }
            }
        }
示例#5
0
        public ActionResult Delete(MarcaEditViewModel marcaVm)
        {
            try
            {
                marcaVm = mapper.Map <MarcaEditViewModel>(servicio.GetMarcaId(marcaVm.MarcaId));

                servicio.Borrar(marcaVm.MarcaId);
                TempData["Msg"] = "Registro borrado...";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, e.Message);
                return(View(marcaVm));
            }
        }
示例#6
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Marca marca = _dbContext.Marcas.SingleOrDefault(m => m.MarcaId == id);

            if (marca == null)
            {
                return(HttpNotFound("Código de marca no encontrado"));
            }

            MarcaEditViewModel marcaVm = ConstruirMarcaEdit(marca);

            return(View(marcaVm));
        }
示例#7
0
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            MarcaEditDto marcaDto = servicio.GetMarcaId(id);

            if (marcaDto == null)
            {
                return(HttpNotFound("Código de Marca inexistente..."));
            }

            MarcaEditViewModel marcaVm = mapper.Map <MarcaEditViewModel>(marcaDto);

            return(View(marcaVm));
        }