{"id":6012,"date":"2014-04-11T23:16:24","date_gmt":"2014-04-11T23:16:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/asp-net-mvc4-mapping-views-to-an-enumerable-with-multiple-concrete-types-collection-of-common-programming-errors\/"},"modified":"2014-04-11T23:16:24","modified_gmt":"2014-04-11T23:16:24","slug":"asp-net-mvc4-mapping-views-to-an-enumerable-with-multiple-concrete-types-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/asp-net-mvc4-mapping-views-to-an-enumerable-with-multiple-concrete-types-collection-of-common-programming-errors\/","title":{"rendered":"ASP.NET MVC4, mapping views to an enumerable with multiple concrete types-Collection of common programming errors"},"content":{"rendered":"<p>A problem I&#8217;ve come into a lot is the following:<\/p>\n<p>Usually I will have an IEnumerable come in from our CMS that will have multiple types, like this:<\/p>\n<pre><code>var headerNavigation = new List() {\n          new TextElement(),\n          new TextElement(),\n          new ImageElement()\n};\n<\/code><\/pre>\n<p>Ideally, I&#8217;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:<\/p>\n<pre><code>foreach (IHeaderNav element in Model)\n{\n    if (sth is TextElement)\n    {\n         @{ Html.RenderPartial(\"TextElement\", element);  }\n    }\n\n    \/\/ etc...\n}\n<\/code><\/pre>\n<p>It would be better if i just passed <code>return View(Model)<\/code> 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?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A problem I&#8217;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&#8217;d like to pass headerNavigation into a View and have MVC find the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6012","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6012"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6012\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}