asp.net-mvc MVC:传入字典的模型项是X类型,但是这个字典需要X类型的模型项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9392816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC: The model item passed into the dictionary is of type X, but this dictionary requires a model item of type X
提问by Obsivus
I am using EF model first and using viewmodels in MVC, I have problems with this error:
我首先使用 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]'.
This is my viewmodel:
这是我的视图模型:
public class IndexCreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname {get;set;}
}
This is my Action in my controller:
这是我在控制器中的操作:
public ActionResult Index()
{
var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
return View(Q.ToList());
}
Here is my strongly typed view:
这是我的强类型视图:
@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>
I dont seem to find the solution for this, do I have to return my viewmodel inside the controller action index? Any help is appreciated.
我似乎没有找到解决方案,我是否必须在控制器操作索引中返回我的视图模型?任何帮助表示赞赏。
Thanks in advance!
提前致谢!
Best Regards!
此致!
回答by silverfighter
See: note down below Would it help to return Enumerable?
请参阅:下面的注释是否有助于返回 Enumerable?
return View(Q.AsEnumerable());
EDIT: Yeah, I know my first shot was wrong!... did not noticed that your view wants a
编辑:是的,我知道我的第一枪是错误的!...没有注意到你的观点想要一个
@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>
I agree with the others about converting to the required return value to the matching type... or adjust the expecting type in the view... maybe you can take advantage of mapping framworks like automapper (http://automapper.org/) to help you to solve to mapping code between domain objects and viewmodels.
我同意其他人关于将所需的返回值转换为匹配类型...或调整视图中的预期类型...也许您可以利用像 automapper (http://automapper.org/) 这样的映射框架帮助您解决域对象和视图模型之间的映射代码。
it's up to you - No need for further down voting...
这取决于你 - 无需进一步投票......
回答by Ofer Zelig
Yes - the action should return a model of the same type declared on the first line of your view file.
是 - 操作应该返回在视图文件的第一行中声明的相同类型的模型。
Change your first line in the view file to:
将视图文件中的第一行更改为:
@model IndexCreateViewModel
and change your action code (Index()) to a code that will return an object of type IndexCreateViewModel.
并将您的操作代码 ( Index()) 更改为将返回类型对象的代码IndexCreateViewModel。
回答by koregan
The type returned by
返回的类型
db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
looks to be IQueryable<NKI3.Models.Question>
看起来是 IQueryable<NKI3.Models.Question>
You need to convert that structure (your data model) to an IEnumerable<IndexCreateViewModel>
您需要将该结构(您的数据模型)转换为 IEnumerable<IndexCreateViewModel>
If there isn't already a helper in your app to transform between them then you'll need to write one.
如果您的应用程序中还没有可以在它们之间进行转换的助手,那么您需要编写一个助手。
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);

