I'm developing an application that manages Courses in ASP.NET MVC, I'm still a beginner, and I'm trying to do the following: The Student has a screen where he lists all the courses for him to choose and make his enrollment in any of these courses. This part I've already done , but I need to make the following control, where, if that student signs up for a course , for example, information systems you will not be able to sign up for this same course because it is already subscribed, the system should bar you and not let it make two entries in the same course.
My Action Enrollment
// GET
public ActionResult Inscricao()
{
//Aqui eu pego o Aluno logado
Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == System.Web.HttpContext.Current.User.Identity.Name);
if (aluno == null)
return View("MeusCursos");
//Aqui ficaria a parte onde verifica se o aluno já está inscrito em algum curso
return View(db.Cursos.ToList());
}
[HttpPost]
public ActionResult Inscricao(int inscricaoId)
{
using (var scope = new TransactionScope())
{
//Aqui eu pego o Aluno logado
Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == System.Web.HttpContext.Current.User.Identity.Name);
if (aluno == null)
return View("MeusCursos");
//Aqui ficaria a parte onde verifica se o aluno já está inscrito em algum curso
var curso = db.Cursos.FirstOrDefault(c => c.Id == inscricaoId);
if (curso == null)
return View("MeusCursos");
var alunoCurso = new AlunoCurso
{
Aluno = aluno,
Curso = curso
};
db.AlunoCursos.Add(alunoCurso);
db.SaveChanges();
curso.Qtd_Vagas--;
db.Entry(curso).State = EntityState.Modified;
db.SaveChanges();
scope.Complete();
}
return View(db.Cursos.ToList());
}
Can anyone help me with this?