/// <summary> /// Get: Adm/group/Create /// </summary> /// <returns></returns> public ActionResult Create(long? id) { var result = CheckCourseRole(id); if (!result.IsValid) return result.ActionResult; // ReSharper disable once PossibleInvalidOperationException var emptyGroup = new Group() {Actived = true, CourseId = id.Value}; InitFormData(emptyGroup, result.Course.Groups.ToList()); return View(emptyGroup); }
private void InitFormData(Group group, List<Group> groups) { //Lấy tất cả các nhóm câu hỏi và gom nhóm chúng var groupedGroups = PopulateGroups(groups); var disableIds = new List<long>(); groups = new List<Group>(); ConvertToTreeStructure(groupedGroups, disableIds, 0, groups); ViewBag.Id = group.CourseId; //Tạo danh sách chọn làm dữ liệu nguồn cho DropDownList ViewBag.ParentGroupId = group.ParentGroupId > 0 ? new SelectList(groups, "GroupId", "GroupName", group.ParentGroupId, disableIds) : new SelectList(groups, "GroupId", "GroupName", (object)null, disableIds); }
private void CheckAllChildCategories(Group group) { var list = DbContext.Groups.Where(s => s.ParentGroupId == group.GroupId).ToList(); foreach (var item in list) { if (item.GroupId == group.ParentGroupId) { ModelState.AddModelError("ParentGroupId", "Bạn không thể chọn danh mục con làm cha của nó"); return; } CheckAllChildCategories(item); } }
private void CheckValidated(Group group) { // Kiểm tra Parent không phải là chính nó if (group.ParentGroupId == group.GroupId) ModelState.AddModelError("ParentGroupId", "Bạn không thể chọn nhóm câu hỏi làm cha của chính nó"); // kiểm tra cha của nó không phải là con của nó CheckAllChildCategories(group); }
private void AddSubgroup(Group group, List<Group> allCates) { group.ChildGroups = allCates.Where(x => x.ParentGroupId == group.GroupId).ToList(); foreach (var subCate in group.ChildGroups) { AddSubgroup(subCate, allCates); } }