1
My application that manages Courses has an area of Administrator where he approves a pupil in a course. That one field that Approves a student is a field booleano, I’m just having trouble with this one field booleano by marking the checkbox, this change to true is not being saved in the database, and when updating the approbation the field remains empty without being marked.
My Action
public ActionResult Aprovar()
{
return View(db.AlunoCursos.Include(ac => ac.Aluno).ToList());
}
[HttpPost]
public ActionResult Aprovar(int id)
{
var alunoCursos = db.AlunoCursos.FirstOrDefault(ac => ac.CursoId == id && ac.Aluno.Usuario == User.Identity.Name);
alunoCursos.Aprovado = !alunoCursos.Aprovado;
db.Entry(alunoCursos).State = EntityState.Modified;
db.SaveChanges();
return View(db.AlunoCursos.Include(ac => ac.Aluno).ToList());
}
My View Aprovar
@model IEnumerable<MeuProjeto.Models.AlunoCurso>
@{
Layout = "/Views/Shared/_Layout.cshtml";
}
<h2>Aprovar Aluno</h2>
@foreach (var item in Model.GroupBy(ac => ac.Curso))
{
<table class="table table-hover">
<thead>
<tr>
<th>
@item.Key.Nome_Curso
</th>
<th>
Aprovado?
</th>
</tr>
<thead>
<tbody>
@foreach (var alunoCurso in item.ToList())
{
<tr>
<td>
@Html.DisplayFor(_ => alunoCurso.Aluno.Nome)
</td>
<td>
@Html.CheckBoxFor(_ => alunoCurso.Aprovado, new { @class = "Aprovado", id = item.Key })
</td>
</tr>
}
</tbody>
</table>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function () {
$(".Aprovado").click(function () {
$.ajax({
url: "Aprovar/",
type: "POST",
data: { id: $(this).attr('id') },
success: function(data) {
alert("Aprovação feita com sucesso!");
}
});
});
});
</script>
}
To the stampede I’m having this error
The dictionary parameters contains a null entry for the id parameter non-null type "System.Int32" for the method System.Web.Mvc.Actionresult Approve (Int32)' at 'Meuprojeto.Controllers.Alunocursoscontroller'. An optional parameter must be a reference type, an annulable type, or be declared as an optional parameter r nName do parameter:. Parameters.
Can someone help me solve this?
I think for the size of the question a little more detailed answer would be needed.
– Marcus Vinicius