Button only work if passed the Id in the URL in ASP.NET MVC

0

I have a problem in my application manager of Courses in ASP.NET MVC.

The scenario is as follows: I have a screen where the "Student" enrolls in a course. By clicking the "enroll" button, it is associated with a course and the number of vacancies of this course will be decremented. The screen Razor looks something like this:

@foreach (var item in Model)
{
    <tr>
       <td>
            @Html.DisplayFor(modelItem => item.Nome_Curso)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sigla)
        </td>
        ...
        <td>
            <div class="btn-group">
                <div class="col-md-offset-2 col-md-10">
                    <a href="@Url.Action("Inscricao", "Curso")">
                        <input type="submit" value="Inscrição" name="inscricao" class="inscricao btn btn-success" data-toggle ="modal", data-target="#modalaviso" data-inscricaoid="@item.CursoId" /></a>
                </div>
            </div>
        </td>
    </tr>

}

In this case, it only works if I pass the Id as a parameter in the URL. Example: My subscription URL is Courses / Inscription , so the button does nothing . Now if I pass the Id parameter it works like so: Courses / Inscription / 1 .

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script>
        $(document).ready(function() {
            $(".inscricao").click(function() {
                $.ajax({
                    type: "POST",
                    url: "~/Curso/Inscricao/" + $(this).data("inscricaoid"),
                    success: function() {
                        $(this).attr("disabled", "disabled");
                    }
                });
            });

        });
    </script>
}

I do not want to be passing the Id in the URL. I need that when the "student" click on the "enrollment" button it will be associated with the course and the decremented vacancies without passing the Id in the URL.

    
asked by anonymous 04.06.2015 / 22:52

1 answer

0

Basically, you are using an Ajax request whose method is POST , but you are not passing data to POST .

Change to the following:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script>
        $(document).ready(function() {
            $(".inscricao").click(function() {
                $.ajax({
                    type: "POST",
                    url: "~/Curso/Inscricao/",
                    data: { inscricaoId: $(this).data("inscricaoid") }
                    success: function() {
                        $(this).attr("disabled", "disabled");
                    }
                });
            });

        });
    </script>
}

As the example Action I answered here , you will need some changes:

[HttpPost]
public ActionResult Inscricao(int inscricaoId)
{
    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 == inscricaoId);
        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());
}
    
05.06.2015 / 00:39