public async Task <IActionResult> Edit(int id, [Bind("EstudanteId,Nome,Idade,Sexo, Cursos")] EstudantesViewModel estudante) { if (id != estudante.EstudanteId) { return(NotFound()); } if (ModelState.IsValid) { try { var estudanteSelecionado = _context.Estudante.Find(estudante.EstudanteId); estudanteSelecionado.Nome = estudante.Nome; estudanteSelecionado.Idade = estudante.Idade; estudanteSelecionado.Sexo = estudante.Sexo; foreach (var item in _context.CursosEstudantes) { if (item.EstudanteId == estudante.EstudanteId) { _context.Entry(item).State = EntityState.Deleted; } } foreach (var item in estudante.Cursos) { if (item.Checked) { _context.CursosEstudantes.Add(new CursoEstudante() { EstudanteId = estudante.EstudanteId, CursoId = item.Id }); } } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EstudanteExists(estudante.EstudanteId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(estudante)); }
// GET: Estudantes/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var estudante = await _context.Estudante .FirstOrDefaultAsync(m => m.EstudanteId == id); var estudanteViewModel = new EstudantesViewModel(); estudanteViewModel.EstudanteId = id.Value; estudanteViewModel.Nome = estudante.Nome; estudanteViewModel.Idade = estudante.Idade; estudanteViewModel.Sexo = estudante.Sexo; var cursos = from c in _context.Curso select new { c.CursoId, c.Nome, Checked = ((from ce in _context.CursosEstudantes where (ce.EstudanteId == id) & (ce.CursoId == c.CursoId) select ce).Count() > 0) }; var checkboxListCursos = new List <CheckBoxViewModel>(); foreach (var item in cursos) { checkboxListCursos.Add(new CheckBoxViewModel { Id = item.CursoId, Nome = item.Nome, Checked = item.Checked }); } estudanteViewModel.Cursos = checkboxListCursos; if (estudanteViewModel == null) { return(NotFound()); } return(View(estudanteViewModel)); }
// GET: Disciplinas public IActionResult Index(string searchString, string sortOrder, bool sortAsc, int page = 1) { var horariosBDContext = _context.Estudante.Include(d => d.EstudanteDisciplina).ToList(); if (!String.IsNullOrEmpty(searchString)) { horariosBDContext = horariosBDContext.Where(s => s.Nome.ToLower().Contains(searchString.ToLower()) || s.Email.ToLower().Contains(searchString.ToLower())).ToList(); } switch (sortOrder) { case "Nome": if (sortAsc) { horariosBDContext = horariosBDContext.OrderBy(d => d.Nome).ToList(); } else { horariosBDContext = horariosBDContext.OrderByDescending(d => d.Nome).ToList(); } break; case "Ano": if (sortAsc) { horariosBDContext = horariosBDContext.OrderBy(d => d.Ano).ToList(); } else { horariosBDContext = horariosBDContext.OrderByDescending(d => d.Ano).ToList(); } break; case "Email": if (sortAsc) { horariosBDContext = horariosBDContext.OrderBy(d => d.Email).ToList(); } else { horariosBDContext = horariosBDContext.OrderByDescending(d => d.Email).ToList(); } break; case "NumeroEstudante": if (sortAsc) { horariosBDContext = horariosBDContext.OrderBy(d => d.NumeroEstudante).ToList(); } else { horariosBDContext = horariosBDContext.OrderByDescending(d => d.NumeroEstudante).ToList(); } break; case "EstadoEstudante": if (sortAsc) { horariosBDContext = horariosBDContext.OrderBy(d => d.EstadoEstudante).ToList(); } else { horariosBDContext = horariosBDContext.OrderByDescending(d => d.EstadoEstudante).ToList(); } break; } var contagemItems = horariosBDContext.Count(); var viewModel = new EstudantesViewModel() { Estudantes = horariosBDContext.Skip(PagingInfo.ItemsPerPage * (page - 1)).Take(PagingInfo.ItemsPerPage).ToList() }; viewModel.PagingInfo.CurrentPage = page; viewModel.PagingInfo.TotalItems = contagemItems; viewModel.PagingInfo.Ascending = sortAsc; viewModel.PagingInfo.CurrentSort = sortOrder; viewModel.PagingInfo.CurrentFilter = searchString; return(View(viewModel)); }