0
I’m trying to update an object and the only thing that doesn’t update is his relationship from many to many the rest as name and surname update
[HttpPost]
public IActionResult AtualizarProfessor(int id, string nome, string sobrenome, List<int> listaDeIdDasTurmas)
{
Professor professor = new Professor(nome, sobrenome);
professor.Id = id;
if (listaDeIdDasTurmas == null || listaDeIdDasTurmas.Count() == 0)
return View();
foreach (var turmaId in listaDeIdDasTurmas)
{
Turma turma = _turmaRepository.ObterTurmaPeloId(turmaId);
if (turma == null || turma.Id == 0)
return View();
var professorturma = new ProfessorTurma();
professorturma.ProfessorId = professor.Id;
professorturma.TurmaId = turma.Id;
professor.Turmas.Add(professorturma);
}
if (!professor.ValidaProfessor())
{
ViewData["Message"] = "Envie os dados do professor de forma correta!";
return View();
}
else
{
_professorRepository.Atualizar(professor);
}
return RedirectToAction("cadastrarprofessor", "Admin");
}
public void Atualizar(Professor professor)
{
using (var contexto = new MinosContext())
{
contexto.Professores.Update(professor);
contexto.SaveChanges();
}
}
in case is not persisting the classes added? Teacher is an entity? or just a model? how is stated the relationship with Classes?
– Leandro Angelo
Good afternoon friend, you can show the code of this context.Update method(teacher);
– Henrique Felipe