public async Task <IActionResult> Create(SubSectionAndSectionViewModel model) { if (ModelState.IsValid) { var doesSubSectionExists = _db.SubSection.Include(s => s.Section).Where(s => s.Name == model.SubSection.Name && s.Section.Id == model.SubSection.SectionId); if (doesSubSectionExists.Count() > 0) { //Error StatusMessage = "Error : Sub Section exists under " + doesSubSectionExists.First().Section.Name + " section. Please use another name."; } else { _db.SubSection.Add(model.SubSection); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubSectionAndSectionViewModel modelVM = new SubSectionAndSectionViewModel() { SectionList = await _db.Section.ToListAsync(), SubSection = model.SubSection, SubSectionList = await _db.SubSection.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; return(View(modelVM)); }
//Get Create public async Task <IActionResult> Create() { SubSectionAndSectionViewModel model = new SubSectionAndSectionViewModel() { SectionList = await _db.Section.ToListAsync(), SubSection = new Models.SubSection(), SubSectionList = await _db.SubSection.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync(), }; return(View(model)); }
//GET - EDIT public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subSection = await _db.SubSection.SingleOrDefaultAsync(m => m.Id == id); if (subSection == null) { return(NotFound()); } SubSectionAndSectionViewModel model = new SubSectionAndSectionViewModel() { SectionList = await _db.Section.ToListAsync(), SubSection = subSection, SubSectionList = await _db.SubSection.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }