Decrease in a field of table Asp.net MVC

Asked

Viewed 123 times

2

I am developing course manager in Asp.net MVC, and in the course table has a field qty vacancies. I wonder how I do to make one Count-- (or some other way to Decrease) this field. For example: In the list of courses will appear the courses and the amount of vacancies and a sign up button. The student by clicking sign up, the field qty vacancies decreasing the number of vacancies in the table and on screen. Courses with 50 vacancies, if an application is made will decrease 49, 48, 47... How I make the field automatically update both table and user screen!?

1 answer

0


I’m guessing you’re using Entity Framework to abstract the database. An example of Action in the Controller it would be something like that:

public ActionResult Inscrever(int id) // Este id é o id do curso
{
    // Aqui penso que ocorreria uma seleção do aluno logado.
    // Depois uma associação do aluno ao curso.
    // Suponho que o curso seria colocado em uma variável "curso".
    curso.QuantidadeVagas--;
    context.Entry(curso).State = EntityState.Modified;
    context.SaveChanges();
    return RedirectToAction("AlgumaAction", "AlgumController");
}

Only that as are various bank operations you do here (select student, select course, create association, modify course), recommend you use transactional scope. Then I’d be like this:

public ActionResult Inscrever(int id) 
{
    using (var scope = new TransactionScope()) 
    {
        // Aqui penso que ocorreria uma seleção do aluno logado.
        // Depois uma associação do aluno ao curso.
        // Suponho que o curso seria colocado em uma variável "curso".
        curso.QuantidadeVagas--;
        context.Entry(curso).State = EntityState.Modified;
        context.SaveChanges();

        scope.Complete(); 
    }

    return RedirectToAction("AlgumaAction", "AlgumController");
}

For display on screen, it is trivial: the course value update will automatically reflect on the presentation:

@model Curso

<h1>Quantidade de Vagas: @Model.QuantidadeVagas</h1>

An example of Ajax would be a button like this:

<button id="decremento" name="1">Decrementar</button>

Add jQuery to your Views (try to use Bundling) and write something like that:

<script>
    $(document).ready(function() {
        $("#decremento").click(function() {
            $.ajax({
              type: "POST",
              url: '/MeuController/Inscrever/' + $(this).attr("name")
            });
        });
    }); 
</script>
  • I’m using the Entity Framework @Gypsy. I am still an apprentice at Asp.net mvc. I would like to know how I associate this decrease with the click of the button.

  • Then you’d have to use Ajax to call Action. The solution code doesn’t change much. Usually jQuery solves this well.

  • you have some example of Ajax?

  • @Newbie Yes, look at the issue I just made.

  • I don’t know what I’m doing wrong @Gypsy, but in my button action nothing is happening. I added Jquery to my View '<script src="@Url.Content("~/Scripts/jquery-2.1.4.js")" type="text/javascript"></script>' and did the same in the example. '<script> $(Document). ready(Function() { $("#inscription").click(Function() { $.ajax({ type: "POST", url: 'Course/Enroll' + $(this).attr("name") }); }); }); </script>'

  • @Newbie If you put a breakpoint on Action of Controller, the execution for it?

  • I put the breakpoint, not there. It does nothing. @Gypsy

  • First you have to be able to call Action. If you haven’t marked the Action with [HttpPost], won’t work.

  • Look at my Action @Gypsy [HttpPost]&#xA; public ActionResult Inscrever(int id)&#xA; {&#xA; var curso = new Curso();&#xA; curso.Qtd_Vagas--;&#xA; db.Entry(curso).State = EntityState.Modified;&#xA; db.SaveChanges();&#xA;&#xA; return PartialView("PartialCursoIndex", db.Cursos);&#xA; }

  • Help me @Gypsy, give a hand here!? Please.

  • @Newbie Ask another question, please. Do not code in comments. Be more objective in your error or problem. Explain how your code is and what has been done to reproduce the problem.

Show 6 more comments

Browser other questions tagged

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