0
votes

I am getting the following error

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>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>
2
Please read the help files to learn how to format your code blocks. Not you html is invalid - your have an opening <p> tag and a closing </table> tag - user3559349
The error is caused by @Html.DisplayNameFor(model => model.CategoryName). Your model is a collection, not an object (IEnumerable does not contain a property CategoryName). Refer my updated answer - user3559349

2 Answers

3
votes

To take advantage of the model binding features of MVC you should create a view model which represents those the properties you want to display and/or edit (adjust property types as required)

public class CategoryVM
{
  public int ID { get; set; } 
  public string CategoryName { get; set; }
  public int Stock { get; set; }
  public DateTime EntryDate { get; set; }
}

and change you method to

public ActionResult Index()
{
  DB db = new DB();
  var categorylist = (from a in db.categories
                 select new CategoryVM
                 {
                   CategoryName = a.CategoryName, 
                   ID = a.ID, 
                   Stock = a.stock, 
                   EntryDate = a.EntryDate
                 }).ToList();
  return View(categorylist);
}

View

@model List<CategoryVM>
<table>
  <thead>
    <tr>
      <th>Category Name</th>
      <th>stock</th>
      <th>Entry Date</th>
      ...
    </tr>
  </thead>
  <tbody>
    @foreach(var item in Model)
    {
      <tr>
        <td>@Html.DisplayFor(m => item.ID)<td>
        <td>@Html.DisplayFor(m => item.CategoryName)</td>
        ...
      </tr>
    }
  </tbody>
</table>
0
votes

I think you need to Change the model type thar you defined in your view

Edit: Try

@model IList<dynamic>