say I have these classes:
public class Animal
{
}
public class Elephant : Animal
{
public string Name { get; set; }
}
and I have a controller method
public SubmitElephants()
{
var elephants = new List<Animal>();
elephants.Add(new Elephant { Name = "Timmy" };
elephants.Add(new Elephant { Name = "Michael" };
return View("DisplayElephants", elephants);
}
The DisplayElephants view looks like:
@model IList<Elephant>
@foreach(var elephant in Model)
{
<div>@elephant.Name</div>
}
So if I run this code I will get the error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[Animal]', but this dictionary requires a model item of type 'System.Collections.Generic.IList
1[Elephant]'
So no I'm NOT wanting to change my list to be var elephants = new List<Elephant>();
What I'm wanting to know given I have a list of Animals that I know contains only Elephants how can I from the controller pass this to a view which is specific to Elephants?