我收到此错误
传递到字典中的模型项是类型的 'Pms_system.Models.MaintenanceCheckListModel',但是这本字典 需要类型的模型项 'System.Collections.Generic.IEnumerable`1 [Pms_system.Models.MaintenanceCheckListModel]'。
我在我的控制器中有这个
public ActionResult MaintenanceCheckList (MaintenanceCheckListModel check)
{
List<MaintenanceCheckListModel> checklist = db.checklists.Select( s => new MaintenanceCheckListModel()
{
FirstMonth = check.FirstMonth,
SecondMonth = check.SecondMonth,
ThirdMonth = check.ThirdMonth,
FourthMonth = check.FourthMonth,
FifthMonth = check.FifthMonth,
SixthMonth = check.SixthMonth,
SeventhMonth = check.SeventhMonth,
EighthMonth = check.EighthMonth,
NinethMonth = check.NinethMonth ,
TenthMonth = check.TenthMonth,
EleventhMonth = check.EleventhMonth,
TwelvethMonth = check.TwelvethMonth
}).ToList();
return View(check);
}
这是我的观点
{
@model IEnumerable<Pms_system.Models.MaintenanceCheckListModel>
@{
ViewBag.Title = "MaintenanceCheckList";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>MaintenanceCheckList</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.SecondMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.ThirdMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.FourthMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.FifthMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.SixthMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.SeventhMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.EighthMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.NinethMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.TenthMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.EleventhMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.TwelvethMonth)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.SecondMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.ThirdMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.FourthMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.FifthMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.SixthMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.SeventhMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.EighthMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.NinethMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.TenthMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.EleventhMonth)
</td>
<td>
@Html.DisplayFor(modelItem => item.TwelvethMonth)
</td>
</tr>
}
错误的解决方案是什么?
答案 0 :(得分:3)
将您的控制器操作更改为以下内容:
public ActionResult MaintenanceCheckList (MaintenanceCheckListModel check)
{
List<MaintenanceCheckListModel> checklist = db.checklists.Select( s => new MaintenanceCheckListModel()
{
FirstMonth = check.FirstMonth,
SecondMonth = check.SecondMonth,
ThirdMonth = check.ThirdMonth,
FourthMonth = check.FourthMonth,
FifthMonth = check.FifthMonth,
SixthMonth = check.SixthMonth,
SeventhMonth = check.SeventhMonth,
EighthMonth = check.EighthMonth,
NinethMonth = check.NinethMonth ,
TenthMonth = check.TenthMonth,
EleventhMonth = check.EleventhMonth,
TwelvethMonth = check.TwelvethMonth
}).ToList();
return View(checklist);
}
您将参数作为视图模型传递,其类型不同。
答案 1 :(得分:1)
这是由于类型不同。您需要将相同的类型传递给您的视图。 根据此
更改您的控制器或视图MaintenanceCheckListModel check
return View(check);
@model Pms_system.Models.MaintenanceCheckListModel
或
List<MaintenanceCheckListModel> checklist = db.checklists.Select( s => new MaintenanceCheckListModel(){}).ToList();
return View(checklist);
@model List<Pms_system.Models.MaintenanceCheckListModel>
修改强>
另外,如果您使用的是List
或Ienumerable
,则必须循环浏览视图中的所有值。
答案 2 :(得分:0)
您正在传递MaintenanceCheckListModel的对象,而您期望IEnumerable。请通过清单而不是从控制器查看。
只需更改返回查看(检查)以返回查看(核对清单);