【发布时间】:2014-11-16 20:02:00
【问题描述】:
我收到以下错误
传入字典的模型项的类型为“System.Collections.Generic.List
1[<>f__AnonymousType34[System.String,System.Int32,System.Int32,System.DateTime]]”,但该字典需要'System.Collections.Generic.IEnumerable`1[ControllerName]' 类型的模型项。
我做了代码迁移(数据库更新)之后我只想按列名列出项目我的代码如下
public ActionResult Index()
{
DB db = new DB();
var categorylist = from a in db.categories
select new
{
a.CategoryName,
a.ID,
a.stock,
a.EntryDate
};
return View(categorylist.ToList());
}
任何帮助将不胜感激
提前致谢。
视图如下
@model IEnumerable<Categories.Models.Categories>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create") </p> <table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.CategoryName)</th>
<th>@Html.DisplayNameFor(model => model.stock)</th>
<th>@Html.DisplayNameFor(model => model.EntryDate)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.CategoryName)</td>
<td>@Html.DisplayFor(modelItem => item.stock)</td>
<td>@Html.DisplayFor(modelItem => item.EntryDate)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
【问题讨论】:
-
请阅读帮助文件以了解如何格式化您的代码块。不是你的 html 是无效的 - 你有一个开始的
<p>标记和一个结束的</table>标记 -
错误是由
@Html.DisplayNameFor(model => model.CategoryName)引起的。您的模型是一个集合,而不是一个对象(IEnumerable不包含属性CategoryName)。参考我更新的答案
标签: asp.net-mvc