ASP.NET MVC4, mapping views to an enumerable with multiple concrete types-Collection of common programming errors
A problem I’ve come into a lot is the following:
Usually I will have an IEnumerable come in from our CMS that will have multiple types, like this:
var headerNavigation = new List() {
new TextElement(),
new TextElement(),
new ImageElement()
};
Ideally, I’d like to pass headerNavigation into a View and have MVC find the corresponding view based on the name of the model. While I could do something like this in my view:
foreach (IHeaderNav element in Model)
{
if (sth is TextElement)
{
@{ Html.RenderPartial("TextElement", element); }
}
// etc...
}
It would be better if i just passed return View(Model)
and have the loop and matching of view and type name done by convention. Is this baked into MVC and I do not know about it? Or is there a contrib project that does this? What would be the general way of going about this? Rewriting the view engine?