public async Task <IActionResult> Create(DefaultModel model, ModProgramEntity item) { ViewBag.Title = "Thêm mới"; var subjectdata = _subjectService.Find(true, o => o.IsActive).OrderBy(o => o.Name).ToList(); ViewBag.SubjectData = subjectdata; var gradedata = _gradeService.Find(true, o => o.IsActive).OrderBy(o => o.Name).ToList(); ViewBag.GradeData = gradedata; if (!string.IsNullOrEmpty(model.ID) || !string.IsNullOrEmpty(item.ID)) { return(RedirectToAction("Edit", new { model.ID })); } else { if (string.IsNullOrEmpty(item.Name)) { SetMessageWarning("Bạn chưa điền tên giáo trình"); return(View()); } else { item.Code = UnicodeName.ConvertUnicodeToCode(item.Name, "-", true); item.Created = DateTime.Now; item.Updated = DateTime.Now; item.IsAdmin = true; item.CreateUser = _currentUser.ID; item.Grades.RemoveAll(o => o == null); item.Subjects.RemoveAll(o => o == null); if (_service.GetItemByCode(item.Code) == null) { await _service.AddAsync(item); SetMessageSuccess("Thêm mới thành công"); return(RedirectToAction("Index")); } else { SetMessageWarning("Giáo trình đã tồn tại"); return(View()); } } } }
public async Task <IActionResult> Edit(DefaultModel model, ModProgramEntity item) { ViewBag.Title = "Cập nhật thông tin"; var subjectdata = _subjectService.Find(true, o => o.IsActive).OrderBy(o => o.Name).ToList(); ViewBag.SubjectData = subjectdata; var gradedata = _gradeService.Find(true, o => o.IsActive).OrderBy(o => o.Name).ToList(); ViewBag.GradeData = gradedata; if (string.IsNullOrEmpty(model.ID) && string.IsNullOrEmpty(item.ID)) { SetMessageWarning("Chưa chọn đối tượng chỉnh sửa"); } else { string ID = !string.IsNullOrEmpty(model.ID) ? model.ID : item.ID; var _item = _service.GetByID(ID); if (string.IsNullOrEmpty(item.Name)) { _item.Name = item.Name; } _item.Description = item.Description; _item.Updated = DateTime.Now; item.Grades.RemoveAll(o => o == null); item.Subjects.RemoveAll(o => o == null); _item.Grades = item.Grades; _item.Subjects = item.Subjects; //TODO: tính toán xem có cần cập nhật lại code ko _item.IsActive = item.IsActive; await _service.AddAsync(_item); ViewBag.Data = _service.GetByID(ID); SetMessageSuccess("Cập nhật thành công"); } ViewBag.Model = model; return(RedirectToAction("index")); }