How to fix System.Collections.Generic.Ienumerable error

Asked

Viewed 2,189 times

0

My application that manages Courses is making that mistake O item de modelo passado para o dicionário é do tipo 'System.Collections.Generic.List'1 [MeuProjeto.Models.Curso] ", mas este dicionário requer um item de modelo do tipo' System.Collections.Generic.IEnumerable'1 [MeuProjeto .Models.AlunoCurso] '. What happens is I have one View who returns to the pupil the courses he is enrolled in, and on that list has a button that is qualified as soon as the administrator approves the student in a course, but when I debugo give this error above.

My Action

public ActionResult MeusCursos()
    {
        Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
        if (aluno != null)
        {
            //O ERRO no DEBUG cai aqui nessa linha.
            return View("MeusCursos", aluno.AlunoCursos.Select(ac => ac.Curso).ToList());
        }

        return View();
    }

My View

@model IEnumerable<MeuProjeto.Models.AlunoCurso>

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

<h2>Meus Cursos</h2>

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

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Curso.Nome_Curso)
            </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", "AlunoCursos")"> <input type="submit" value="Emitir Declaração" name="meusCursos" class="cursos btn btn-success" enable="enable" /> </a>*@
                            <a href="~/AlunoCursos/[email protected]" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-print"></span></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>
}

1 answer

1


You are creating a new list of Cursos and not of AlunoCurso with that line:

return View("MeusCursos", aluno.AlunoCursos.Select(ac => ac.Curso).ToList());

Within your .Select you take only the courses and return as a new list is where your problem lies, because in your View you defined your @model as:

@model IEnumerable<MeuProjeto.Models.AlunoCurso>

To resolve this you need to remove the .Select(ac => ac.Curso) or change the @model for @model IEnumerable<MeuProjeto.Models.Curso> and adapt your screen code.

  • So @Maicon, it’s because on my membership table Hallucinogenic has the fields Alunoid, Cursoid and Approved. I need to take the field Approved from this table and list the courses.

  • and what’s the problem with returning Hallucinogenic ?

  • It worked @Maicon, I had changed the code in the wrong place. I can talk to you via chat?

  • Yes we can, come on

  • Call me on Chat @Maicon, I still don’t know what to call you!

Show 1 more comment

Browser other questions tagged

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