【问题标题】:Error on listing the elements列出元素时出错
【发布时间】:2014-11-16 20:02:00
【问题描述】:

我收到以下错误

传入字典的模型项的类型为“System.Collections.Generic.List1[<>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 是无效的 - 你有一个开始的 &lt;p&gt; 标记和一个结束的 &lt;/table&gt; 标记
  • 错误是由@Html.DisplayNameFor(model =&gt; model.CategoryName)引起的。您的模型是一个集合,而不是一个对象(IEnumerable 不包含属性CategoryName)。参考我更新的答案

标签: asp.net-mvc


【解决方案1】:

要利用 MVC 的模型绑定功能,您应该创建一个视图模型来表示您想要显示和/或编辑的那些属性(根据需要调整属性类型)

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

并将您的方法更改为

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);
}

查看

@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>

【讨论】:

    【解决方案2】:

    我认为您需要更改您在视图中定义的模型类型

    编辑:尝试

    @model IList<dynamic>
    

    【讨论】:

    • 感谢您的回复.....我将类型从 IEnumerable 更改为 Ilist 但仍然出现错误:(
    • 您也可以发表您的看法吗?
    • 试试 '@model IList'
    • 收到此错误“System.Collections.Generic.IList”不包含“CategoryName”的定义,并且没有接受“System.Collections”类型的第一个参数的扩展方法“CategoryName”。 Generic.IList' 可以找到(您是否缺少 using 指令或程序集引用?)
    • 好的,就像我之前说的,请发布您的查看代码:-)
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多