Lets say I have two classes one base class:
public class BaseModel {
}
And a couple of children:
public class FooModel : BaseModel {
}
public class BarModel : BaseModel {
}
Now my view I would like to expect a model like this:
@model IList<BaseModel>
So that I can edit these models on one page.
However when I pass in a collection of these models in the action I get an exception saying:
The model item passed into the dictionary is of type
‘System.Collections.Generic.List1[BarModel]', but this dictionary1[BaseModel]’.
requires a model item of type
'System.Collections.Generic.IList
Like this:
var models = new List<BaseModel>(){ new BarModel(), new FooModel ()}
return View(models);
How can I achieve this?
Thanks
Assuming you’re not modify the model collection on the page, your page shouldn’t take
IList<T>. Rather it should takeIEnumerable<T>:That should take care of things.