简体   繁体   English

从控制器到视图的MVC传递列表

[英]MVC passing list from controller to view

I have a controller that returns a list of view models, like so: 我有一个控制器,它返回视图模型列表,如下所示:

public ActionResult Index()
{
   List<DailyPlanListViewModel> viewModels = new List<DailyPlanListViewModel>();
   //do some stuff with the list
   return View(viewModels);
}

and a view that takes the list and should display the information 以及带有列表并应显示信息的视图

@model List<IEnumerable<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel>>

BUT I get this error, because of the IEnumerable type: 但是由于IEnumerable类型,我收到此错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[System.Collections.Generic.IEnumerable`1[D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]]'. 传递到字典中的模型项的类型为'System.Collections.Generic.List1 [D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]',但是此字典需要类型为'System.Collections.Generic.List1 [System]的模型项.Collections.Generic.IEnumerable`1 [D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel]]”。

I can't get it to work. 我无法正常工作。 What can I do? 我能做什么?

You need to remove the IEnumerable part: 您需要删除IEnumerable部分:

@model List<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel>

Your list is the IEnumerable. 您的列表是IEnumerable。

A better way to approach this would be to create another class to wrap this list, so you're only passing one model back to the view instead of a list of models. 解决此问题的更好方法是创建另一个类来包装此列表,因此您只需将一个模型传递回视图,而不是模型列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM