传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [SecundaireSchool.Models.tblRapporten]'

时间:2016-01-13 18:52:17

标签: asp.net asp.net-mvc entity-framework

传递到字典中的模型项的类型为System.Collections.Generic.List'1[SecundaireSchool.Models.tblRapporten],但此字典需要类型为SecundaireSchool.Models.tblRapporten的模型项。

模型 tblStudenten

public partial class tblStudenten
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblStudenten()
    {
        this.tblRapportens = new HashSet<tblRapporten>();
    }

    public int student_id { get; set; }
    public string naam { get; set; }
    public string voornaam { get; set; }
    public string emailadres { get; set; }
    public string foto { get; set; }
    public int klas_id { get; set; }

    public virtual tblKlassen tblKlassen { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<tblRapporten> tblRapportens { get; set; }
    public virtual IEnumerable<SelectListItem> Klassen { get; set; }
}

模型 tblRapporten

public partial class tblRapporten
{
    public int rapport_id { get; set; }
    public double punt { get; set; }
    public int vak_id { get; set; }
    public int student_id { get; set; }

    public virtual tblStudenten tblStudenten { get; set; }
    public virtual tblVakken tblVakken { get; set; }
}

StudentController

public ActionResult Report(int id)
    {
        return View(db.tblRapportens.Where(r => r.student_id == id).ToList());
    }

查看报告

@model SecundaireSchool.Models.tblRapporten

@{
    ViewBag.Title = "Report";
}

<h2>Report</h2>

<div>
<h4>tblRapporten</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.punt)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.punt)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.tblStudenten.naam)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.tblStudenten.naam)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.tblVakken.vak)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.tblVakken.vak)
    </dd>

    </dl>
</div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.rapport_id }) |
        @Html.ActionLink("Back to List", "Index")
    </p>

我知道我的控制器列表有问题,但我不知道如何解决这个问题。 任何人吗?

1 个答案:

答案 0 :(得分:1)

您的视图期望模型是单个SecundaireSchool.Models.tblRapporten对象,但您的控制器正在为您的模型分配List<SecundaireSchool.Models.tblRapporten>。在您的视图中将您的模型类型更改为@model IList<SecundaireSchool.Models.tblRapporten>,然后您就可以开展业务。