View does not render in ASP.NET MVC

1

Well, I'm new to asp.net MVC and I'm picking up some time already for a blessed View . According to the help I had in this post #

Following the example I made my adaptations but now my View is not rendering. Can someone give me strength?!

I made an Action GET returning View and the page rendered, however, the action of my button does nothing, nor does it enter breakpoint .

    //GET
    public ActionResult Inscricao()
    {
        return View(db.Cursos);
    }

    //POST
    [HttpPost]
    public ActionResult Inscricao(int inscricaoId)
    {
        using (var scope = new TransactionScope())
        {
            Aluno aluno = db.Alunos.FirstOrDefault();
            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(db.Cursos.ToList());

    }

POST error

CourseList

My button after the click should also be disabled as per the code below. It's just that it's not disabling.

<script>
    $(document).ready(function() {
        $("#inscricao").click(function() {
            $.ajax({
                type: "POST",
                url: "Inscricao/",
                data: {inscricaoId: $(this).data("inscricaoid")},
                success: function() {
                        $(this).attr("disabled", "disabled");
                    }
            });
        });
    });
</script>
    
asked by anonymous 05.06.2015 / 20:54

1 answer

1

UPDATE

Replace

<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>

By

<div class="btn-group">
    <div class="col-md-offset-2 col-md-10">
        @using (Html.BeginForm("Inscricao", "Curso", FormMethod.Post))
        {
            <a class="inscricao btn btn-success" onclick="$(this).parents('form').submit()">Inscrição</a>
            <input type="hidden" value="@item.CursoId" name="inscricaoId" />
        }
    </div>
</div>

That way you do not even need the script you created earlier, and you do everything in one request.

-

-

By analyzing the code of the other question, you probably left the code in this line:

<a href="@Url.Action("Inscricao", "Curso")">

With the new script you do not need it anymore because you already make the POST request at the time you click on input . Please remove it from the page.

-

Old solution:

Seeing your error is wrong in the same ajax request, switch

url: "~/Curso/Inscricao/",

by

url: "Inscricao/",

Getting

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

        });
    </script>
}
    
06.06.2015 / 00:10