View does not render on Asp.net MVC

Asked

Viewed 500 times

1

Guys, I’m a beginner in asp.net MVC and I’m already picking up a certain amount of time for a blessed View. According to the help I had in this post Button only works if you pass the ID in the URL in Asp.net MVC

Following the example I made my adaptations but now mine View is not rendering. Someone can give me a strength?!

So I did an action GET returning to View and the page rendered, however, the action of my button does nothing, nor enters the 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());

    }

Erro POST inserir a descrição da imagem aqui

List of Courses inserir a descrição da imagem aqui

My button after clicking should also be disabled as code below. Only it is 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>
  • 2

    Add the codes that calls the View

  • What appears in the browser log console when you click the button?

  • Appeared 3 errors GET @Maiconcarraro

  • Shows which are

1 answer

1


UPDATE

Substitute

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

For

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

So you don’t even need the script you created earlier and do it all in one request.

-

-

Analyzing the code of the other question, you probably left node code that line:

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

With the new script no longer need it because you already make the request by POST at the time you click on input. Remove it from the page.

-

Old solution:

Seeing by your mistake is wrong in the same ajax request, exchange

url: "~/Curso/Inscricao/",

for

url: "Inscricao/",

Staying

@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>
}
  • I took this line there, now is giving this error in the console I will post the question @Maiconcarraro

  • @Newbie Updated with the answer

  • Valeu @Maiconcarraro worked, however, when the number of vacancies is decreasing the page does not update automatically I have to press F5.

  • @Newbie Where are you displaying the vacancies?

  • I will post the question @Maiconcarraro along with Ajax that calls this View

  • is still the same.

  • I’m thinking of another solution, to put a form around your button or simply redirect the page when you receive the success

  • @Newbie I added window.location.reload(); within the success, i am going home now when I get there put the other solution with form.

  • @Newbie Ready, updated with the other solution

Show 5 more comments

Browser other questions tagged

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