Edit field within a list in Asp.net MVC

Asked

Viewed 955 times

4

Guys, I’m developing an application that manages Courses, and I’m trying to make a list where present on the screen the Courses, the Student Name and the countryside Approved, where it indicates whether it is approved or not, and on that screen I have to be able to mark the checkbox. The problem is that I’m not getting on the list of Student Name and I can’t even edit the field directly on that list Approved, because when I pass the mouse cursor it is locked and I can not check, the editing of this field on this screen is essential.

inserir a descrição da imagem aqui

My Controller Course Action

    public ActionResult MeusCursos()
    {
        Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
        if (aluno != null)
            return View("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 asked this question here too Problem with Entity Framework Relationship

  • +Newbie, would you write down the class code in the question, please? This would help us better understand the problem of the student’s name not appearing on the list.

1 answer

1


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

As for the students' names not appearing in the code @Html.DisplayFor(modelItem => item.AlunoCursos) Are you sure that Student Courses is the name of the field corresponding to the name in the Student class? For example, if the code of 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) for @Html.DisplayFor(modelItem => item.Nome) (realize that I replaced Student for Name, that in this case would be the correct field of the name in the Student class).

  • You don’t have to. I think that’s right. Just the problem with the student’s name.

  • So I changed my answer with more information. I think that’s it too. Let’s keep waiting for an answer from him to confirm. Thank you.

Browser other questions tagged

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