Problem with a Boolean field on Asp.net MVC

Asked

Viewed 494 times

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?

3 answers

0

Place the property int?.

This is because you need to have some value in the field, if it is mandatory you need to pass this value, otherwise put as null.

  • I think for the size of the question a little more detailed answer would be needed.

0


You are probably passing the wrong amount on this assignment:

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

Your method Aprovar(int id) expects to receive an ID of Hallucinogenic to search for this student in the database, but you are probably passing another variable that is not a int. From what I’ve seen, the right thing would be:

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

That way, you’d be passing the ID of Hallucinogenic correct for the method Aprovar(int id).

  • now he is saving, but not in the Student I selected to mark as approved

  • Can you help me @Alvaro ?

0

Try it like 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>
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.