异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List`

时间:2015-09-11 12:53:48

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

模型:StudentData(模型1)

namespace Aug16.Models
{
    [Table("Stdnt_Info")]
    public class StudentData
    {
        [Key]
        public long Stdnt_Id { get; set; }

        public string Stdnt_Name { get; set; }
        public string Stdnt_Fname { get; set; }
        public string Stdnt_Address { get; set; }

        public string Stdnt_Semmester { get; set; }
        public DateTime Sem_StartDate { get; set; }
        public DateTime Sem_EndDate { get; set; }

        public int Stdnt_Mark1 { get; set; }
        public int Stdnt_Mark2 { get; set; }
        public int Stdnt_Mark3 { get; set; }
        public Decimal Stdnt_Sem_Per { get; set; }
    }
}
-------------------------------------------------------------------------------

DBContext Model:Aug16Data(Model2)

namespace Aug16.Models
{
    public class Stdnt_Details : DbContext
    { 
        public DbSet<StudentData> Student_Information { get; set; }
        //public DbSet<Stdnt_Details> Student_Information { get; set; }
        //public DbSet<Aug16Data> Stdnt_Mark { get; set; }
    }
}
------------------------------------------------------------------------------

Controller:StudentDataController

namespace Aug16.Controllers
{
    public class StudentDataController : Controller
    {
        //
        // GET: /StudentData/

        Aug16.Models.Stdnt_Details StoreDB = new Models.Stdnt_Details();

        public ActionResult Aug16DataAction()
        {
            var StdDet = StoreDB.Student_Information.ToList();
            return View(StdDet);
        }
    }
}
---------------------------------------------------------------------------

查看:Aug16DataAction

@model IEnumerable<Aug16.Models.Stdnt_Details>

@{
    ViewBag.Title = "Aug16DataAction";
}

<h2>Aug16DataAction</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

错误如下

  应用程序中的服务器错误。

     

传递到字典中的模型项是'System.Collections.Generic.List [Aug16.Models.StudentData]'类型,但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable [8月16日]的模型项。 Models.Stdnt_Details]”。

请问这个错误的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

首先,您需要更改您的模型类型,如您对问题的评论所述。

但是,您正在查看空视图,因为您没有显示所有模型属性的代码。尝试使用以下代码更改您的观点:

@model IEnumerable<Test.Models.StudentData>

@{
    ViewBag.Title = "Aug16DataAction";
}

<h2>Aug16DataAction</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Fname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Semmester)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sem_StartDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sem_EndDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark3)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Sem_Per)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Fname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Semmester)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Sem_StartDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Sem_EndDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark3)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Sem_Per)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Stdnt_Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Stdnt_Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Stdnt_Id })
            </td>
        </tr>
    }

</table>