How to link an Id to Asp.net MVC

Asked

Viewed 139 times

1

Guys, I have a little problem with my application, which is a course manager. The problem is that "student" has to enroll in some course, only I’m not able to make the student’s association to the course. On my registration screen I have a button "enrollment", where, the student clicks and this button should associate the "student" to that particular course, in addition to associate, this button also makes the decrease of vacancies of that course according to what students will enroll.

I’m trying to do it like this
Meu Controller

public ActionResult Inscricao(int? id)
    {
        using (var scope = new TransactionScope())
        {

            // Aqui pegaria o aluno logado.
            // E aqui é onde tenho que fazer a associação do aluno ao curso.

            Curso curso = new Curso();

            //Tentei fazer assim, mas não consegui
            curso = db.Cursos.Include(a => a.Aluno).AsNoTracking().FirstOrDefault(c => c.Id == id);

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

            scope.Complete();
        }

        return View("Inscricao", db.Cursos.ToList());

    }
  • Related: http://answall.com/questions/67387/listagem-no-asp-net-mvc/67474

1 answer

0


As I explained to you here, how modeling has changed, what needs to be done is to create an object of association between Aluno and Curso and then decrease the amount of vacancies of a Curso.

That is to say:

public ActionResult Inscricao(int? id)
{
    using (var scope = new TransactionScope())
    {

        // Aqui pegaria o aluno logado.
        var aluno = db.Alunos.FirstOrDefault(/* Aqui vai uma condição */);
        if (aluno == null) return View("Inscricao", db.Cursos.ToList());

        var curso = db.Cursos.FirstOrDefault(c => c.Id == id);
        if (curso == null) return View("Inscricao", db.Cursos.ToList());

        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("Inscricao", db.Cursos.ToList());
}
  • It worked @Gypsy, only now I have another problem. When I go to the Signup screen, the "signup" button only decreases and associates the guy if I pass the Id to the URL. Example: Course/Signup So it doesn’t work, now if I do so Course/Registration/1 works. How do I get it to decrement and associate without having to pass the ID in the URL? I ask another question?

  • @Newbie Yes, ask another question. Involves using POST and a few more adjustments to Action.

  • Veja link ai @Cigano http://answall.com/questions/67560/bot%C3%A3o-s%C3%B3-funciona-se-passar-o-id-na-url-no-Asp-net-mvc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.