0

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?

5
  • 1
    @model IEnumerable<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel> Commented Nov 19, 2015 at 10:24
  • 1
    @model List<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel> Commented Nov 19, 2015 at 10:24
  • 1
    try @model List<D2D.Web.ViewModels.DailyPlan.DailyPlanListViewModel Commented Nov 19, 2015 at 10:24
  • 1
    List<IEnumerable<...>> should be quite obvious what the error is getting at. Commented Nov 19, 2015 at 10:25
  • thx to all, i finally got it working Commented Nov 19, 2015 at 11:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.