How to edit a Boolean field in a list in Asp.Net MVC

Asked

Viewed 287 times

1

Guys, I’m developing an application that manages Courses, and I have the following problem. On my screen Administrator I need the fields Course Name, Student Name and the countryside Approved which is a field booleano. That screen is where the Administrator tells which pupil is approved in the course you signed up for. Only in my View only fields appear Course Name and the countryside Approved and nay appears the Student Name, as shown below. inserir a descrição da imagem aqui

The other problem is that this field of mine Approved when I mark the checkbox he’s not saving like true in the Database, is always getting false.

Action Aprovacao of controller Course

public ActionResult Aprovacao()
    {
        return View(db.Cursos.ToList());
    }

My View Aprovacao

@model IEnumerable<MeuProjeto.Models.Curso>

@{
    Layout = "/Views/Shared/_Layout.cshtml";
}

<h2>Aprovação</h2>

<table class="table table-hover">
    <tr>
        <th>
            Curso
        </th>
        <th>
            Aluno
        </th>
        <th>
            Aprovado?
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nome_Curso)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AlunoCursos)
            </td>
            <td>
                @*@Html.DisplayFor(modelItem => item.Aprovado)*@
                <input type="checkbox" id="Aprovado" name="Aprovado" value="Aprovado"/>
            </td>
        </tr>
    }

</table>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

}

1 answer

0


As far as I can tell, there’s nothing that fires a Action to the Controller, then your View needs some modifications:

1. Identify the CheckBox correctly

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nome_Curso)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AlunoCursos)
        </td>
        <td>
            @Html.CheckBox("Aprovado_" + item.Nome_Curso, new { @class = "Aprovado", id = item.Id })
        </td>
    </tr>
}

2. Add Script that shoots Action

This can be done by Ajax:

$(".Aprovado").click(function() {
    $.ajax({
        url: '@Url.Action("Aprovacao", "Curso")',
        type: "POST",
        data: { id = $(this).attr('id') },
        success: function (data) {
            alert("Aprovação feita com sucesso!");
        }
   });
});

3. Create a Action Aprovar in AlunoCursosController

[HttpPost]
public ActionResult Aprovar(int id)
{
    var alunoCurso = db.AlunoCursos.FirstOrDefault(ac => ac.AlunoCursoId == id);
    alunoCurso.Aprovado = !alunoCurso.Aprovado;
    db.Entry(alunoCurso).State = EntityState.Modified;
    db.SaveChanges();
}

About the column Aluno

When it comes to selecting AlunoCursos, try the following:

public ActionResult Aprovacao()
{
    return View(db.AlunoCursos.Include(ac => ac.Aluno).ToList());
}

To display:

@Html.DisplayFor(modelItem => item.AlunoCursos.Aluno.Nome)
  • Hey @Gypsy this code here var alunoCursos = contexto.AlunoCursos.Include(ac => ac.Aluno).ToList(); goes in my controller right?

  • @Beginner This. Just send to View alunoCursos.

  • My field Approved still not saving @Gypsy, when I update the page o checkbox empty turn, and the bank is also not saving as true.

  • @Rookie Already put a breakpoint on Action that Ajax calls?

  • No, I’ll do it now @Gypsy

  • I think the problem is here [HttpPost]&#xA; public ActionResult Aprovar(int id)&#xA; {&#xA; var alunoCursos = db.AlunoCursos.Include(ac => ac.Aluno).ToList();&#xA;&#xA; return View(alunoCursos);&#xA; }. Can you shed some light on this part here // Select here the Alunocurso and change the Approved.. I did not...

  • The selection of alunoCursos does not go into Aprovar. See my edition. Inside Aprovar will select only one alunoCurso and there you edit the boolean.

  • Give me just one example @Gypsy of how I would make this selection of a alunoCurso within the Aprovar and edit my field booleano, because here I could not.

  • @Newbie Edited the answer.

  • here on this stretch alunoCurso.Aprovado = !alunoCurso.Aprovado; error of Nullreferenceexception.

  • @Newbie It means that alunoCurso is null, and who did not find the record by id past. You are performing debug of the code you are writing?

  • Yes @Gypsy, when debugging here in this section var alunoCurso = db.AlunoCursos.FirstOrDefault(ac => ac.AlunoCursoId == id); it features a id = 5

Show 8 more comments

Browser other questions tagged

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