Edit field within a list in ASP.NET MVC

4

Well, I'm developing an application that manages Courses , and I'm trying to list where the Courses displays, the Student Name > and the Approved field, where it indicates whether it is approved or not, and on this screen I have to be able to check the checkbox . The problem is that I am not able to make the Student Name list on this list and I can not even edit the Approved field in this list, because when I move the mouse cursor it is ok blocked and I can not check, editing this field on this screen is a must.

MyControllerActionCourse

publicActionResultMeusCursos(){Alunoaluno=db.Alunos.FirstOrDefault(a=>a.Usuario==User.Identity.Name);if(aluno!=null)returnView("MeusCursos", db.Cursos.ToList());

        return View();
    }

    [HttpPost]
    public ActionResult MeusCursos(int id)
    {
        Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
        if (aluno != null)
            return View("MeusCursos", db.Cursos.ToList());

        var curso = db.Cursos.FirstOrDefault(c => c.Id == id);
        if (curso == null)
            return View("MeusCursos");

        if (curso.Aprovado == false)
        {
            ModelState.AddModelError("Aprovado", "Você ainda não concluiu o Curso");
            return RedirectToAction("MeusCursos");
        }

        return View(db.Cursos.ToList());
    }

My View

@model IEnumerable<MeuProjeto.Models.Curso>

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

<h2>Meus Cursos</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)
            </td>
            <td>
                <div class="btn-group">
                    <div class="col-md-offset-2 col-md-10">
                        @if (item.Aprovado == false)
                        {
                            <input type="submit" value="Pendente de Aprovação" name="meusCursos" class="cursos btn btn-default" disabled="disabled" data-id="@item.Id"/>
                        }
                        else
                        {
                            <a href="@Url.Action("GerarPDF", "Aluno")"> <input type="submit" value="Emitir Declaração" name="meusCursos" class="cursos btn btn-success" enable="enable" /> </a>
                        }
                    </div>
                </div>
            </td>
        </tr>
    }

</table>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script>
        $(document).ready(function() {
            $(".cursos").click(function() {
                $.ajax({
                    type: "POST",
                    url: "MeusCursos/",
                    data: { id: $(this).data("id") },
                    success: function() {
                        $(this).attr("enable", "enable");
                    }
                });
            });
        });
    </script>
}

For more details I've asked this question here too Problem with relationship Entity Framework

    
asked by anonymous 14.06.2015 / 06:12

1 answer

1

For the field to be an editable checkbox, try replacing @Html.DisplayFor(modelItem => item.Aprovado) with @Html.EditorFor(modelItem => item.Aprovado) . DisplayFor is for presentation only, while EditorFor generates a field for editing.

As for the name of the students not appearing, in the code @Html.DisplayFor(modelItem => item.AlunoCursos) are you sure that StudentCourses is the name of the field corresponding to the name in the Student class? For example, if the code in your Student class is something similar to

public class Aluno
{
    // Campo nome do aluno.
    public string Nome { get; set; }
}

Then you have to replace @Html.DisplayFor(modelItem => item.AlunoCursos) with @Html.DisplayFor(modelItem => item.Nome) (notice that I replaced StudentsCourses with Name , which in this case would be the correct field of the name in the Student class).

    
08.07.2015 / 18:13