Ejemplo n.º 1
0
        public ActionResult CreateCarModel(CreateCarModelViewModel item)
        {
            string fileName = string.Empty;

            if (Request.Files.Count > 0)
            {
                foreach (string it in Request.Files)
                {
                    HttpPostedFileBase file      = Request.Files[it];
                    string             fname     = System.IO.Path.GetFileName(file.FileName);
                    string             extension = System.IO.Path.GetExtension(file.FileName);
                    if (fname != string.Empty && extension != string.Empty)
                    {
                        fileName = "/Content/Cars/" + item.Name + extension;
                        fileName = Regex.Replace(fileName, @"\s", "_");
                        file.SaveAs(Server.MapPath(fileName));
                    }
                    else
                    {
                        fileName = "/Content/Cars/No_image.jpg";
                    }
                }
            }

            var mapper = new MapperConfiguration(cg => cg.CreateMap <CreateCarModelViewModel, CarModelDTO>()).CreateMapper();
            var model  = mapper.Map <CreateCarModelViewModel, CarModelDTO>(item);

            int?manufacturerId = _manufacturerService.GetManufacturers().ToList().Find(it => it.Name == item.ManufacturerName).Id;

            if (manufacturerId != null)
            {
                model.ManufacturerId = manufacturerId.Value;
            }

            int?qualityId = _qualityClassService.GetQualityClasses().ToList().Find(it => it.Name == item.QualityClassName).Id;

            if (qualityId != null)
            {
                model.QualityClassId = qualityId.Value;
            }
            model.Photo = fileName;
            _carModelService.CreateCarModel(model);
            return(RedirectToAction("CreateCarModel"));
        }