示例#1
0
        // GET: Xes/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var xe = await _context.Xe
                     .Include(a => a.DongXe)
                     .Include(b => b.Tinh)
                     .Include(c => c.LoaiXe)
                     .FirstOrDefaultAsync(d => d.XeId == id);

            if (xe == null)
            {
                return(NotFound());
            }

            var editXe = new EditXeViewModel
            {
                XeId            = xe.XeId,
                NamSx           = xe.NamSx,
                ImageDauXe1     = xe.ImageDauXe,
                ImageNoiThatXe1 = xe.ImageNoiThatXe,
                ImageThanXe1    = xe.ImageThanXe,
                ImageDuoiXe1    = xe.ImageDuoiXe,
                DaThue          = xe.DaThue,
                DangKinhDoanh   = xe.DangKinhDoanh,
                MauXeId         = _context.MauXe.FirstOrDefault(m => m.TenMauXe == xe.Mau).MauXeId,
                LoaiXeId        = xe.LoaiXeId,
                TinhId          = xe.TinhId,
                Tittle          = xe.Tittle,
                DongXeId        = xe.DongXeId,
                GiaTheoGio      = xe.GiaTheoGio,
                GiaTheoNgay     = xe.GiaTheoNgay
            };

            ViewData["DongXeId"]     = new SelectList(_context.DongXe, "DongXeId", "TenDongXe");
            ViewData["NhaSanXuatId"] = new SelectList(_context.NhaSanXuat, "NhaSanXuatId", "TenNSX");
            ViewData["LoaiXeId"]     = new SelectList(_context.LoaiXe, "LoaiXeId", "TenLoaiXe");
            ViewData["MauXeId"]      = new SelectList(_context.MauXe, "MauXeId", "TenMauXe");
            ViewData["TinhId"]       = new SelectList(_context.Tinh, "TinhId", "TenTinh");

            return(View(editXe));
        }
示例#2
0
        public async Task <IActionResult> Edit(EditXeViewModel model)
        {
            if (ModelState.IsValid)
            {
                Xe editXe = _context.Xe.Find(model.XeId);
                editXe.Tittle        = model.Tittle;
                editXe.TinhId        = model.TinhId;
                editXe.Rate          = model.Rate;
                editXe.NamSx         = model.NamSx;
                editXe.LoaiXeId      = model.LoaiXeId;
                editXe.GiaTheoNgay   = model.GiaTheoNgay;
                editXe.GiaTheoGio    = model.GiaTheoGio;
                editXe.DaThue        = model.DaThue;
                editXe.DangKinhDoanh = model.DangKinhDoanh;

                if (model.DongXeId != 0)
                {
                    editXe.DongXeId = model.DongXeId;
                }
                if (model.MauXeId != 0)
                {
                    editXe.Mau = _context.MauXe.Find(model.MauXeId).TenMauXe;
                }

                if (model.ImageDauXe != null)
                {
                    if (model.ImageDauXe1 != null)
                    {
                        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "images", model.ImageDauXe1);
                        System.IO.File.Delete(filePath);
                    }
                    editXe.ImageDauXe = ProcessUpLoadedFile(model);
                }

                if (model.ImageDuoiXe != null)
                {
                    if (model.ImageDuoiXe1 != null)
                    {
                        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "images", model.ImageDuoiXe1);
                        System.IO.File.Delete(filePath);
                    }
                    editXe.ImageDuoiXe = ProcessUpLoadedFile1(model);
                }
                if (model.ImageNoiThatXe != null)
                {
                    if (model.ImageNoiThatXe1 != null)
                    {
                        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "images", model.ImageNoiThatXe1);
                        System.IO.File.Delete(filePath);
                    }
                    editXe.ImageNoiThatXe = ProcessUpLoadedFile2(model);
                }
                if (model.ImageThanXe != null)
                {
                    if (model.ImageThanXe1 != null)
                    {
                        string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "images", model.ImageThanXe1);
                        System.IO.File.Delete(filePath);
                    }
                    editXe.ImageThanXe = ProcessUpLoadedFile3(model);
                }



                _context.Update(editXe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["DongXeId"]     = new SelectList(_context.DongXe, "DongXeId", "TenDongXe");
            ViewData["NhaSanXuatId"] = new SelectList(_context.NhaSanXuat, "NhaSanXuatId", "TenNSX");
            ViewData["LoaiXeId"]     = new SelectList(_context.LoaiXe, "LoaiXeId", "TenLoaiXe");
            ViewData["MauXeId"]      = new SelectList(_context.MauXe, "MauXeId", "TenMauXe");
            ViewData["TinhId"]       = new SelectList(_context.Tinh, "TinhId", "TenTinh");
            return(View(model));
        }