I am getting the following error
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[<>f__AnonymousType34[System.String,System.Int32,System.Int32,System.DateTime]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ControllerName]'.
I did the code migration (database update) after that I want to just list the items by column name My code is as follows
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());
}
Any help would be appreciated
Thanks in advance.
View is as follows
@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>
<p>tag and a closing</table>tag - user3559349@Html.DisplayNameFor(model => model.CategoryName). Your model is a collection, not an object (IEnumerabledoes not contain a propertyCategoryName). Refer my updated answer - user3559349