How to fix the System.Collections.Generic.IEnumerable error

0

My application that manages Courses has this error The template item passed to the dictionary is of type 'System.Collections.Generic.List'1 [MyProject.Models.Course] ", but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1 [MyProject.Models.CustomerName]' . What happens is that I have a View that returns to the student the courses he is enrolled in, and on that list there is a button that is enabled as soon as the administrator the student in a course , but when I debugo gives 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>
}
    
asked by anonymous 27.06.2015 / 19:28

1 answer

1

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

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

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

@model IEnumerable<MeuProjeto.Models.AlunoCurso>

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

    
27.06.2015 / 19:34