Пример #1
0
        public ActionResult Create([Bind(Include = "Id,Nome,Descricao,DataLimiteInscricao,ProfessorId")] Curso curso)
        {
            if (ModelState.IsValid)
            {
                db.Cursos.Add(curso);
                db.SaveChanges();

                int primaryKey = curso.Id;

                CursoProfessor professor = new CursoProfessor();

                for (int i = 0; i < curso.ProfessorId.Count(); i++)
                {
                    professor.cursoId = primaryKey;
                    professor.professorId = Convert.ToInt32(curso.ProfessorId[i]);
                    db.CursoProfessor.Add(professor);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            return View(curso);
        }
Пример #2
0
        public ActionResult Edit([Bind(Include = "Id,Nome,Descricao,DataLimiteInscricao, ProfessorId")] Curso curso)
        {
            if (ModelState.IsValid)
            {
                //deletando os cursos
                var professores = db.CursoProfessor.Where(x => x.cursoId == curso.Id);
                foreach (var p in professores)
                {
                    db.CursoProfessor.Remove(p);
                }
                db.SaveChanges();

                if (curso.ProfessorId != null)
                {
                    //inserindo os cursos
                    CursoProfessor prof = new CursoProfessor();

                    for (int i = 0; i < curso.ProfessorId.Count(); i++)
                    {
                        prof.cursoId = curso.Id;
                        prof.professorId = Convert.ToInt32(curso.ProfessorId[i]);
                        db.CursoProfessor.Add(prof);
                        db.SaveChanges();
                    }

                }

                db.Entry(curso).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Edit/" + curso.Id);

                /*db.Entry(curso).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");*/
            }
            return View(curso);
        }