marcus
marcus

Reputation: 333

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:

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]]'.

I can't get it to work. What can I do?

Upvotes: 0

Views: 2700

Answers (1)

Darren Gourley
Darren Gourley

Reputation: 1808

You need to remove the IEnumerable part:

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

Your list is the 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.

Upvotes: 2

Related Questions