【问题标题】:MVC 3 - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1MVC 3 - 传入字典的模型项的类型为“System.Collections.Generic.List”1
【发布时间】:2013-01-28 17:15:24
【问题描述】:

您好,我是 mvc 新手,需要帮助 我创建了这个

public ActionResult Index()
        {
            var joblist = (from s in _entities.TaleoJobs
                           group s by new { s.JobTitle}
                               into myGroup
                               where myGroup.Count() > 0
                               select new { myGroup.Key.JobTitle }
                               );
            return View(joblist.ToList()); 
        }

但是当我创建视图时出现以下错误

传入字典的模型项的类型为“System.Collections.Generic.List1[<>f__AnonymousType01[System.String]]”,但此字典需要类型为“System.Collections.Generic.IEnumerable”的模型项`1[careers.TaleoJobs]'。

这是视图的代码

*@model IEnumerable<careers.TaleoJobs>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>

        <th>
            JobTitle
        </th>

    </tr>
@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.JobTitle)
        </td>

    </tr>
}
</table>*

如果有人能提供帮助,我将不胜感激 - 尝试查看其他示例,但我不知所措。

【问题讨论】:

    标签: asp.net-mvc linq


    【解决方案1】:

    当您选择列表时,您只选择了 JobTitle,它是一个字符串。所以你的名单确实是List&lt;string&gt;

    您可以更新您的选择以选择整个对象:

    var joblist = (from s in _entities.TaleoJobs
                               group s by new { s.JobTitle}
                                   into myGroup
                                   where myGroup.Count() > 0
                                   select s
                                   );
    

    或者,保持当前选择并将视图类型更新为:

    IEnumerable<string>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2011-11-26
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多