コード例 #1
0
        public ActionResult CreateOrUpdate(HopViewModel model)
        {
            var hop = string.IsNullOrEmpty(model.Id) ? new Hop {
                NgayCapNhat = DateTime.Now
            }
                : _hopService.Get(m => m.Id == model.Id);

            var    autoList        = AutoCompleteTextKes(GetKes());
            string userId          = User.Identity.GetUserId();
            string chiTietHoatDong = "hộp: " + hop.ChuyenDe;

            hop.ChuyenDe    = model.ChuyenDe;
            hop.KeId        = autoList.FirstOrDefault(p => p.Text == model.KeId).Id;
            hop.PhongBanId  = model.PhongBanId;
            hop.SoHop       = model.SoHop;
            hop.UserId      = userId;
            hop.NgayBatDau  = model.NgayBatDau;
            hop.NgayKetThuc = model.NgayKetThuc;

            // create new
            if (string.IsNullOrEmpty(model.Id))
            {
                DropDownList();

                var checkName = _hopService.Get(m => m.ChuyenDe == model.ChuyenDe);

                // check name
                if (checkName != null)
                {
                    TempData["AlertMessage"] = "Hộp Chuyên Đề Đã Tồn Tại";
                    return(View(model));
                }

                hop.TinhTrang = EnumTinhTrang.TrongKho;
                var result = UpdateTu_SoHopHienTai(hop.KeId, ActionWithObject.Update);

                // check amout ke
                if (!result)
                {
                    TempData["AlertMessage"] = "Số Lượng Hộp Trong Kệ Bạn Chọn Đã Đầy";
                    return(View(model));
                }
                _hopService.Insert(hop);

                _functionLichSuHoatDongService.Create(ActionWithObject.Create, userId, chiTietHoatDong);

                TempData["AlertMessage"] = "Tạo Mới Thành Công";
            }
            else // update
            {
                _hopService.Update(hop);

                _functionLichSuHoatDongService.Create(ActionWithObject.Update, userId, chiTietHoatDong);
                TempData["AlertMessage"] = "Cập Nhật Thành Công";
            }
            return(RedirectToAction("Index"));
        }