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.
– Novato
Then you’d have to use Ajax to call
Action
. The solution code doesn’t change much. Usually jQuery solves this well.– Leonel Sanches da Silva
you have some example of Ajax?
– Novato
@Newbie Yes, look at the issue I just made.
– Leonel Sanches da Silva
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>'
– Novato
@Newbie If you put a breakpoint on
Action
ofController
, the execution for it?– Leonel Sanches da Silva
I put the breakpoint, not there. It does nothing. @Gypsy
– Novato
First you have to be able to call
Action
. If you haven’t marked theAction
with[HttpPost]
, won’t work.– Leonel Sanches da Silva
Look at my Action @Gypsy
[HttpPost]
 public ActionResult Inscrever(int id)
 {
 var curso = new Curso();
 curso.Qtd_Vagas--;
 db.Entry(curso).State = EntityState.Modified;
 db.SaveChanges();

 return PartialView("PartialCursoIndex", db.Cursos);
 }
– Novato
Help me @Gypsy, give a hand here!? Please.
– Novato
@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.
– Leonel Sanches da Silva