public async Task <IActionResult> Edit(int id, [Bind("Id,ProfesorId,IdiomaId")] ProfesorIdioma profesorIdioma)
        {
            if (id != profesorIdioma.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(profesorIdioma);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProfesorIdiomaExists(profesorIdioma.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IdiomaId"]   = new SelectList(_context.Idioma, "Id", "Id", profesorIdioma.IdiomaId);
            ViewData["ProfesorId"] = new SelectList(_context.Profesor, "Id", "Id", profesorIdioma.ProfesorId);
            return(View(profesorIdioma));
        }
        public async Task <IActionResult> Create([Bind("Id,GeneroId,Nombre,Apellidos,Imagen")] Profesor profesor,
                                                 List <int> formaciones, List <int> idiomas, List <int> materias)
        {
            Usuario usuario = await _userManager.GetUserAsync(User);

            profesor.UsuarioId = usuario.Id;

            profesor.ProfesoresFormaciones = new List <ProfesorFormacion>();
            profesor.ProfesoresIdiomas     = new List <ProfesorIdioma>();
            profesor.ProfesoresMaterias    = new List <ProfesorMateria>();

            foreach (int idIdioma in idiomas)
            {
                ProfesorIdioma profesorIdioma = new ProfesorIdioma
                {
                    ProfesorId = profesor.Id,
                    IdiomaId   = idIdioma
                };
                profesor.ProfesoresIdiomas.Add(profesorIdioma);
            }

            foreach (int idFormacion in formaciones)
            {
                ProfesorFormacion profesorFormacion = new ProfesorFormacion
                {
                    ProfesorId  = profesor.Id,
                    FormacionId = idFormacion
                };
                profesor.ProfesoresFormaciones.Add(profesorFormacion);
            }

            foreach (int idMateria in materias)
            {
                ProfesorMateria profesorMateria = new ProfesorMateria
                {
                    ProfesorId = profesor.Id,
                    MateriaId  = idMateria
                };
                profesor.ProfesoresMaterias.Add(profesorMateria);
            }

            if (ModelState.IsValid)
            {
                _context.Add(profesor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProfesoresMateriasId"]    = new SelectList(_context.Set <Materia>(), "Id", "Nombre", profesor.ProfesoresMaterias);
            ViewData["ProfesoresIdiomasId"]     = new SelectList(_context.Set <Idioma>(), "Id", "Nombre", profesor.ProfesoresIdiomas);
            ViewData["ProfesoresFormacionesId"] = new SelectList(_context.Set <Formacion>(), "Id", "Nombre", profesor.ProfesoresFormaciones);
            ViewData["GeneroId"] = new SelectList(_context.Set <Genero>(), "Id", "Nombre", profesor.GeneroId);
            return(View(profesor));
        }
        public async Task <IActionResult> Create([Bind("Id,ProfesorId,IdiomaId")] ProfesorIdioma profesorIdioma)
        {
            if (ModelState.IsValid)
            {
                _context.Add(profesorIdioma);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IdiomaId"]   = new SelectList(_context.Idioma, "Id", "Id", profesorIdioma.IdiomaId);
            ViewData["ProfesorId"] = new SelectList(_context.Profesor, "Id", "Id", profesorIdioma.ProfesorId);
            return(View(profesorIdioma));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,GeneroId,Nombre,Apellidos,Imagen")] Profesor profesor,
                                               List <int> formaciones, List <int> idiomas, List <int> materias)
        {
            if (id != profesor.Id)
            {
                return(NotFound());
            }

            List <ProfesorFormacion> profesorFormaciones = await _context.ProfesorFormacion
                                                           .Where(pf => pf.ProfesorId == profesor.Id)
                                                           .ToListAsync();

            foreach (ProfesorFormacion formacion in profesorFormaciones)
            {
                _context.Remove(formacion);
            }

            List <ProfesorIdioma> profesorIdiomas = await _context.ProfesorIdioma
                                                    .Where(pi => pi.ProfesorId == profesor.Id)
                                                    .ToListAsync();

            foreach (ProfesorIdioma idioma in profesorIdiomas)
            {
                _context.Remove(idioma);
            }

            Usuario usuario = await _userManager.GetUserAsync(User);

            profesor.UsuarioId = usuario.Id;

            profesor.ProfesoresFormaciones = new List <ProfesorFormacion>();

            profesor.ProfesoresIdiomas = new List <ProfesorIdioma>();


            foreach (int idIdioma in idiomas)
            {
                ProfesorIdioma profesorIdioma = new ProfesorIdioma
                {
                    ProfesorId = profesor.Id,
                    IdiomaId   = idIdioma
                };
                profesor.ProfesoresIdiomas.Add(profesorIdioma);
            }

            foreach (int idFormacion in formaciones)
            {
                ProfesorFormacion profesorFormacion = new ProfesorFormacion
                {
                    ProfesorId  = profesor.Id,
                    FormacionId = idFormacion
                };
                profesor.ProfesoresFormaciones.Add(profesorFormacion);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(profesor);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProfesorExists(profesor.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProfesoresMateriasId"]    = new SelectList(_context.Set <Materia>(), "Id", "Nombre", profesor.ProfesoresMaterias);
            ViewData["ProfesoresIdiomasId"]     = new SelectList(_context.Set <Idioma>(), "Id", "Nombre", profesor.ProfesoresIdiomas);
            ViewData["ProfesoresFormacionesId"] = new SelectList(_context.Set <Formacion>(), "Id", "Nombre", profesor.ProfesoresFormaciones);
            ViewData["GeneroId"] = new SelectList(_context.Set <Genero>(), "Id", "Nombre", profesor.GeneroId);
            return(View(profesor));
        }