My application that manages Courses has an area of Administrator where it approves a student in a course . This field that Approves a student is a booleano
field, but I'm having trouble with this field booleano
when tagging checkbox
, this change to true
is not being saved in database , and updating the approval page will leave the field empty unchecked.
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>
}
debbugar I'm having this error
The parameters dictionary contains a null entry for the parameter 'id' of the non-null type "System.Int32" for the method 'System.Web.Mvc.ActionResult Approve (Int32)' in 'MyProject.Controllers.AlumniCoursesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter \ r \ nName parameter:. Parameters.
Can anyone help me solve this?