【问题标题】:MVC: The model item passed into the dictionary is of type X, but this dictionary requires a model item of type XMVC:传入字典的模型项是X类型的,但是这个字典需要X类型的模型项
【发布时间】:2012-03-12 15:56:09
【问题描述】:

我首先使用 EF 模型并在 MVC 中使用视图模型,我遇到了这个错误的问题:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.

这是我的视图模型:

public class IndexCreateViewModel
{
    public string QuestionText { get; set; }
    public string Sname { get; set; }
    public string Cname {get;set;}
}

这是我在控制器中的操作:

public ActionResult Index()
{
    var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
    return View(Q.ToList());   
}

这是我的强类型视图:

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            QuestionText
        </th>
        <th>
            Sname
        </th>
        <th>
            Cname
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Cname)
        </td>

    </tr>
}

</table>

我似乎没有找到解决方案,我是否必须在控制器操作索引中返回我的视图模型?任何帮助表示赞赏。

提前致谢!

最好的问候!

【问题讨论】:

  • 看起来 db.Question 返回 NKI3.Models.Question 而不是您在上面显示的 ViewModel。

标签: asp.net-mvc asp.net-mvc-3 razor mvvm


【解决方案1】:

请参阅:请注意以下 返回 Enumerable 有帮助吗?

 return View(Q.AsEnumerable()); 

编辑:是的,我知道我的第一枪是错的!...没有注意到你的观点想要一个

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

我同意其他人关于将所需返回值转换为匹配类型的观点...... 或调整视图中的预期类型... 也许您可以利用 automapper (http://automapper.org/) 之类的映射框架来帮助您解决域对象和视图模型之间的映射代码。

这取决于你 - 无需进一步投票......

【讨论】:

  • 我试了一下,得到了这个错误:传入字典的模型项的类型是'System.Data.Objects.ObjectQuery1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[NKI3.ViewModels.IndexCreateViewModel]'。跨度>
  • 我不得不使用映射但我没有使用 automapper 现在它工作得很好
  • 我忘记了视图顶部的@model IEnumerable&lt;... 并得到了类似的错误,不得不重新检查我错过了什么。好帖子的家伙。
【解决方案2】:

返回的类型

db.Question.Include(a =&gt; a.SubjectType).Include(a =&gt; a.CoreValue);

看起来是IQueryable&lt;NKI3.Models.Question&gt;

您需要将该结构(您的数据模型)转换为IEnumerable&lt;IndexCreateViewModel&gt;

如果您的应用中还没有帮助器在它们之间进行转换,那么您需要编写一个。

IList<IndexCreateViewModel> viewmodel = new List<IndexCreateViewModel>();

foreach(NKI3.Models.Question item in Q)
{
   IndexCreateViewModel viewItem = new IndexCreateViewModel();
   // I don't know the properties of Question.
   viewItem.QuestionText = item.????;
   viewItem.CName = item.???;
   viewItem.SName =-item.???;
   viewModel.Add(viewItem);
}
return View(viewModel);

【讨论】:

  • 问题是Question实体只有QID和QuestionText。所以 ViewItem.Cname = item.Cname 不起作用。 CoreValue 和 SubjectType 实体具有 Cname 和 Sname 值。
  • 我认为您需要退后一步,看看您的数据模型如何与您的视图模型相关。
【解决方案3】:

是的 - 该操作应返回与视图文件第一行中声明的类型相同的模型。

将视图文件中的第一行更改为:

@model IndexCreateViewModel

并将您的操作代码 (Index()) 更改为将返回 IndexCreateViewModel 类型的对象的代码。

【讨论】:

  • 午饭后我会试试这个,我会报告它是怎么回事:)
  • 不起作用,错误:您不能对 NKI3.ViewModels.IndexCreateViewModel 类型的变量使用 foreach 语句,因为 NKI3.ViewModels.IndexCreateViewModel 不包含 GetEnumerator 的公共定义
  • 您应该将IndexCreateViewModel 更改为其他事物的IEnumerable(例如,创建一个名为Question 的新类,它将成为IEnumerable 中的一个项目)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2015-07-05
相关资源
最近更新 更多