public async Task <ActionResult> CreateMauHopDong(CreateEditMauHopDongVM model)
        {
            model = await AddLoaiHopDongToCreateEditMauHopDongVM(model);


            if (!ModelState.IsValid)
            {
                return(View());
            }

            var mauHopDongMoi = _mapper.Map <MauHopDong>(model);

            mauHopDongMoi.MaNhanVienLuuMauHopDong = userManager.GetUserAsync(HttpContext.User).Result.Id;
            mauHopDongMoi.NgayGuiMauHopDong       = DateTime.Now;
            mauHopDongMoi.ViTriLuuMauHopDong      = UploadMauHopDongLaoDong(model);
            mauHopDongMoi.MaMauHopDong            = Guid.NewGuid().ToString();

            if (mauHopDongMoi.ViTriLuuMauHopDong == null)
            {
                ModelState.AddModelError("", "Error while uploading the MauHopDongFile");
            }

            bool IsSuccess = await _mauHopDongRepository.Create(mauHopDongMoi);

            if (!IsSuccess)
            {
                ModelState.AddModelError("", "error while creating MauHopDong Record on database");
                return(View());
            }

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> CreateMauHopDong()
        {
            var model = new CreateEditMauHopDongVM();

            model = await AddLoaiHopDongToCreateEditMauHopDongVM(model);

            return(View(model));
        }
        private string UploadMauHopDongLaoDong(CreateEditMauHopDongVM model)
        {
            string uniqueFileName = null;

            if (model.FileMauHopDong != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "mauHopDongLaoDongs");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.FileMauHopDong.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.FileMauHopDong.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
        public async Task <ActionResult> EditMauHopDong(CreateEditMauHopDongVM model)
        {
            try
            {
                model = await AddLoaiHopDongToCreateEditMauHopDongVM(model);

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var mauHopDong = _mapper.Map <MauHopDong>(model);

                var uriMauHopDongFile = UploadMauHopDongLaoDong(model);
                if (uriMauHopDongFile != null)
                {
                    var oldMauHopDongName = mauHopDong.ViTriLuuMauHopDong;
                    mauHopDong.ViTriLuuMauHopDong = uriMauHopDongFile;
                    DeleteFileMauHopDong(oldMauHopDongName);
                }
                mauHopDong.MaNhanVienChinhSuaLanCuoi = userManager.GetUserAsync(HttpContext.User).Result.Id;
                mauHopDong.ThoiGianChinhSuaLanCuoi   = DateTime.Now;

                var isSuccess = await _mauHopDongRepository.Update(mauHopDong);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something Went Wrong...");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something Went Wrong...");
                return(View(model));
            }
        }
        private async Task <CreateEditMauHopDongVM> AddLoaiHopDongToCreateEditMauHopDongVM(CreateEditMauHopDongVM model)
        {
            var loaihopdongs = (await loaiHopDongRepository.FindAll())
                               .Select(q => new SelectListItem()
            {
                Text  = q.TenLoai,
                Value = q.MaLoaiHopDong
            });

            model.LoaiHopDongs = loaihopdongs;

            return(model);
        }