Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:47:00+00:00 2026-05-23T10:47:00+00:00

I have an IEnumerable of a base type as my model. I need to

  • 0

I have an IEnumerable of a base type as my model.

I need to display a different bit of HTML in a list depending on what the concrete type is.

So the resulting list might look similar to this in HTML:

<ul>
  <li class="points">Points - Item 1 - 10 points <a href="#">Remove</a></li>
  <li class="media">Media - Item 2 - your_uploaded_image.jpg <a href="#">Remove</a></li>
  <li class="content">Content - Item 3 <a href="#">Remove</a></li>
</ul>

It’s likely I will add another type to this later so solutions like the following aren’t really what I’m after.

@foreach(var item in Model)
{
   if(item is PointImpl)
   {
       var pointItem = item as PointImpl;
       <li class="points">Points - @pointItem.Name - @pointItem.Points points <a href="#">Remove</a></li>
   }
   else if(item is MediaImpl)
   {
       var mediaItem = item as MediaImpl; 
       <li class="media">Media - @mediaItem.Name - @mediaItem.FileName  <a href="#">Remove</a></li>
   }
   /*
       More implementations
   */
}

I’ve had a look at model metadata template hint but that doesn’t really help because my model is an IEnumerable..

I was thinking about a custom Html Helper looking at an attribute on the concrete type but think there might be a built in way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:47:00+00:00Added an answer on May 23, 2026 at 10:47 am

    Instead of the ugly foreach simply use display templates:

    @model IEnumerable<SomeBaseViewModel>
    <ul>
        @Html.DisplayForModel()
    </ul>
    

    and then define display templates for all child types. For example:

    ~/Views/Shared/DisplayTemplates/PointImpl.cshtml:

    @model PointImpl
    <li class="points">
        Points - @Model.Name - @Model.Points points 
        <a href="#">Remove</a>
    </li>
    

    and: ~/Views/Shared/DisplayTemplates/MediaImpl.cshtml:

    @model MediaImpl
    <li class="media">
        Media - @Model.Name - @Model.FileName 
        <a href="#">Remove</a>
    </li>
    

    See, no more ifs, no more loops, no more vars. Everything works by convention (templates must be situated in the ~/Views/Shared/DisplayTemplates or ~/Views/SomeController/DisplayTemplates folder and should be named as the name of the concrete type – PointImpl.cshtml, MediaImpl.cshtml, …). Based on the concrete type the corresponding display template will be rendered and this automatically for each element of the main model collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# Cannot convert from IEnumerable<Base> to IEnumerable<Derived> I have D1 and D2
Possible Duplicate: getting type T from IEnumerable<T> I Have a property type of IEnumerable
I have an HTML helper that I need to pass an Object to. This
I am getting the following error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An
We have a model association that looks something like this: class Example < ActiveRecord::Base
I have a static member in the base class of type: private static Dictionary<string,
I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object
I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of
I have an IEnumerable object. I would like to access based on index for
I have an IEnumerable<Person> object. The Person class has a Designation property. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.