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

1

Well, I'm developing an application that manages Courses , and I have the following problem. On my Administrator screen I need the Course Name , Student Name fields and the Approved field that is a booleano field. This screen is where the Administrator says which student is approved in the course they enrolled in. However, only the Course Name fields and the Approved and no fields appear in my View / strong>, as shown below.

TheotherproblemisthatthisApprovedfieldwhenIsetcheckboxisnotsavingastrueinDatabase,isalwaysgettingfalse.

ActionApprovedofcontrollerCourse

publicActionResultAprovacao(){returnView(db.Cursos.ToList());}

MyViewAprovacao

@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") }     
asked by anonymous 22.06.2015 / 23:03

1 answer

0

As far as I can understand, there is no something that triggers an Action for the Controller , so your View needs some modifications: p>

1. Identify 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 triggers 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 an 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 column Aluno

When you select 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)
    
22.06.2015 / 23:37