1
I am developing an application that manages enrollment in courses, I am still an apprentice at Asp.net MVC, and I have the following question:
On my course screen is a "registration" button for the courses listed on this screen. I would like to know how I do to disable the button when the user signs up, IE, click on "registration" and also not allow registration again in the same course.
Detail: This button also decreases the number of vacancies.
Follows code of decrement.
public ActionResult Confirmacao(int? id)
{
using (var scope = new TransactionScope())
{
Curso curso = new Curso();
curso = db.Cursos.Include(x => x.Aluno).AsNoTracking().FirstOrDefault(f => f.Id == id);
curso.Qtd_Vagas--;
db.Entry(curso).State = EntityState.Modified;
db.SaveChanges();
scope.Complete();
}
return View(db.Cursos);
}
This is Ajax where I call Action
<script>
$(document).ready(function() {
$("#inscricao").click(function() {
$.ajax({
type: "POST",
url: "~/Curso/Confirmacao/" + $(this).attr("Qtd_Vagas")
});
});
});
</script>
HTML Razor
@model IEnumerable<MeuProjeto.Models.Curso>
<h2>Lista de Cursos</h2>
<table class="table table-hover">
<tr>
<th>
Curso
</th>
<th>
Sigla
</th>
<th>
Ementa
</th>
<th>
Inicio
</th>
<th>
Fim
</th>
<th>
Turno
</th>
<th>
Status
</th>
<th>
Quantidade de Vagas
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome_Curso)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sigla)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ementa)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dt_Inicio)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dt_Fim)
</td>
<td>
@Html.DisplayFor(modelItem => item.Turno)
</td>
<td>
<input type="text" name="Status" id="Status" value="@Html.DisplayFor(modelItem => item.Status)" readonly class="Status" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Qtd_Vagas)
</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" id="inscricao" name="inscricao" class="btn btn-success" data_toggle ="modal", data_target="#modalaviso" /></a>
</div>
</div>
</td>
</tr>
}
</table>
<div class="form-group">
<a href="@Url.Action("Index", "Home")"><input type="button" value="Voltar" class="btn btn-danger" /></a>
</div>
<br />
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function() {
$("#inscricao").click(function() {
$.ajax({
type: "POST",
url: "~/Curso/Inscricao/" + $(this).attr("Qtd_Vagas"),
success: function() {
$(this).attr("disabled", "disabled");
}
});
});
});
</script>
}
Didn’t work @Gypsy, just add that code snippet below?
– Novato
No. You need to readjust the code to the need of your screen, test, check if the callback is triggered.
– Leonel Sanches da Silva
Do you have an example? For me to have a sense of how to do?
– Novato
Please put the Razor HTML in your question. Only then can I get an idea of how to activate the button correctly.
– Leonel Sanches da Silva
I put the HTML @Gypsy.
– Novato
@Newbie I updated the answer.
– Leonel Sanches da Silva
It didn’t work @Gypsy. I don’t know if it’s because there’s something wrong with my decrement code, because for the sign-up button I’m having to pass the ID number in the URL. Example: Course/Registration/1 so it works, if I enter the Course/Registration page without passing the Id does not work.
– Novato