异常详细信息:System.InvalidOperationException:传入字典的模型项的类型是'Sys
本文关键字:类型 Sys 模型 InvalidOperationException System 异常 字典 详细信息 | 更新日期: 2023-09-27 18:12:15
Model: StudentData (Model 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; }
}
}
------------------------------------------------------------------------------
控制器:studentdatcontroller
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. list]。',但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable[Aug16.Models.Stdnt_Details]'的模型项。
这个错误的解决方法是什么?
首先,您需要更改您的模型类型,正如在您的问题的注释中提到的。
然而,您正在寻找一个空视图,因为您没有显示所有模型属性的代码。尝试使用以下代码更改视图:
@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>