예제 #1
0
        public IActionResult Edit(int id)
        {
            var employees = employeesRepository.Gets(id);

            if (employees == null)
            {
                return(View("~/Views/Error/Error.cshtml", id));
            }
            var emp = new HomeEditViewsModel()
            {
                AvatarPath         = employees.AvartarPath,
                CarbrandId         = employees.CarbrandId,
                Name               = employees.Name,
                Price              = employees.Price,
                EmployeeId         = employees.Id,
                SelectDescriptions = employees.Descriptions,
                Color              = employees.Color,
                Register           = employees.Register,
                Yeard              = employees.Yeard,
                Gallery            = employees.Gallery
            };

            ViewBag.Gallerys     = (from g in context.Gallerys where g.EmployeeId == emp.EmployeeId select g).ToList();
            ViewBag.Avatar       = employees.AvartarPath;
            ViewBag.Descriptions = GetDescriptions();
            ViewBag.Carbrands    = GetCarbrands();

            return(View(emp));
        }
예제 #2
0
        public async Task <IActionResult> Edit(HomeEditViewsModel model)
        {
            if (ModelState.IsValid)
            {
                var emp = new EditEmployeeViewsModel()
                {
                    AvatarPath   = model.AvatarPath,
                    CarbrandId   = model.CarbrandId,
                    Descriptions = model.Descriptions,
                    Name         = model.Name,
                    Price        = model.Price,
                    EmployeeId   = model.EmployeeId,
                    Color        = model.Color,
                    Register     = model.Register,
                    Yeard        = model.Yeard
                };

                if (model.GalleryFiles != null)
                {
                    string folder = "gallery/";
                    model.Gallery = new List <GalleryModel>();
                    foreach (var gall in model.GalleryFiles)
                    {
                        var images = new GalleryModel()
                        {
                            Name = gall.FileName,
                            Url  = await UploadImage(folder, gall)
                        };
                        model.Gallery.Add(images);
                    }
                    emp.Gallery = model.Gallery;
                }
                if (!string.IsNullOrEmpty(model.Gallery.FirstOrDefault().Name))
                {
                    List <Gallery> galleries = (from e in context.Gallerys where e.EmployeeId == model.EmployeeId select e).ToList();
                    foreach (var tim in galleries)
                    {
                        var fi = Path.Combine(webHostEnvironment.WebRootPath, "gallery", tim.Name);
                        System.IO.File.Delete(fi);
                    }

                    context.Gallerys.RemoveRange(galleries);
                    context.SaveChanges();
                }

                var filename = string.Empty;
                if (model.Avatar != null)
                {
                    string file = Path.Combine(webHostEnvironment.WebRootPath, "images");
                    filename = $"{Guid.NewGuid()}_{model.Avatar.FileName}";
                    var load = Path.Combine(file, filename);
                    using (var stream = new FileStream(load, FileMode.Create))
                    {
                        model.Avatar.CopyTo(stream);
                    }
                    emp.AvatarPath = filename;
                    if (!string.IsNullOrEmpty(model.AvatarPath))
                    {
                        string fil = Path.Combine(webHostEnvironment.WebRootPath, "images", model.Avatar.FileName);
                        System.IO.File.Delete(fil);
                    }
                    //emp.AvatarPath = filename;
                }
                var empl = employeesRepository.Edit(emp);
                if (empl != null)
                {
                    return(RedirectToAction("table", "Home", new { id = empl.EmployeeId }));
                }
            }
            return(View());
        }