【问题标题】:MVC 3 model item passed into dictionary errorMVC 3 模型项传入字典错误
【发布时间】:2012-01-10 15:07:20
【问题描述】:

我收到此错误,但我不明白为什么: 传入字典的模型项的类型为“Devart.Data.Linq.DataQuery`1[CHRContext.WIKIIDEE]”,但此字典需要类型为“CHRContext.WIKIREPARTO”的模型项。

模型方法

public IQueryable<WIKIIDEE> GetIdeasByDeptID(int id)
        {
            var query = from i in db.WIKIIDEEs
                        where i.IDREPARTO == id
                        select i;

            return query;
        }

来自控制器的方法

public ActionResult List(int id)
    {
        try
        {
            IdeeRepository ideeRepo = new IdeeRepository();

            IQueryable<WIKIIDEE> list = ideeRepo.GetIdeasByDeptID(id);

            return View(list);
        }
        catch (Exception Ex)
        {
            return View("Error");
        }
    }

并查看

 @model IEnumerable<CHRContext.WIKIIDEE>

@{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>List</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            IDREPARTO
        </th>
        <th>
            DATAINSERIMENTO
        </th>
        <th>
            DESCRIZIONE
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.IDREPARTO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DATAINSERIMENTO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DESCRIZIONE)
        </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>

【问题讨论】:

  • 你在哪一行得到错误?在控制器中?
  • 当视图中的 foreach 完成对元素的迭代时

标签: asp.net-mvc razor


【解决方案1】:

我遇到了一个非常相似的问题,并且能够解决它。您可以在我的回答中看到如何:The model item passed into the dictionary is of type A, but this dictionary requires a model item of type B

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多