Problem with a Boolean field in ASP.NET MVC

1

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?

    
asked by anonymous 25.06.2015 / 07:18

3 answers

0

You're probably passing the wrong value on this assignment:

@Html.CheckBoxFor(_ => alunoCurso.Aprovado, new { @class = "Aprovado", id = item.Key })

Your Aprovar(int id) method expects to receive a StudentCourse ID to fetch this student from the database, but you are probably passing another variable that is not an int . From what I saw, the correct one would be:

@Html.CheckBoxFor(_ => alunoCurso.Aprovado, new { @class = "Aprovado", id = alunoCurso.CursoId })

In this way, you would be passing the ID of the current StudentCourse correctly to the Aprovar(int id) method.

    
26.06.2015 / 00:45
0

Put the int? property.

This is because it needs to have some value in the field, if it is necessary to pass this value, otherwise put as it accepts null.

    
25.06.2015 / 22:33
0

Try this:

 @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", value = 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).val() },
                success: function(data) {
                    alert("Aprovação feita com sucesso!");
                }
            });
        });
    });

</script>
}
    
25.06.2015 / 13:20