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 8221145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:48:09+00:00 2026-06-07T13:48:09+00:00

I’ve got a list of for example Vehicle’s that I want to render in

  • 0

I’ve got a list of for example Vehicle’s that I want to render in a single page, but have different view characteristics for each vehicle type.

Vehicle
– VehicleId
– VehicleTypeId (Car/Boat/Truck/etc…)
– Description
– …

I’d like to on my main AllVehiclesPage

@foreach(var vehicle in Model) //where Model is a List<Vehicle> from AllVehicles
{
    //Render proper View
    //CarView(vehicle)
    //BoatView(vehicle)
    //TruckView(vehicle)
}

In each view it will render differently depending on the vehicle, IE Wheel rim size applies to cars/trucks not to boats etc…

I’ve read a number of the other questions on Stackoverflow about view inheritance, but they don’t seem to apply.

Is this best to do with partial Views? Should I create Usercontrols?

  • 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-06-07T13:48:10+00:00Added an answer on June 7, 2026 at 1:48 pm

    Use display templates. Replace your entire foreach loop with the following line of code:

    @model IEnumerable<Vehicle>
    ...
    @Html.DisplayForModel()
    

    And now comes the interesting part. You define display templates for each type that you want to handle:

    ~/Views/Shared/DisplayTemplates/Car.cshtml
    ~/Views/Shared/DisplayTemplates/Boat.cshtml
    ~/Views/Shared/DisplayTemplates/Truck.cshtml
    

    Each template will be strongly typed to the corresponding concrete type. So here’s what will happen on the Html.DisplayForModel() line:

    1. ASP.NET MVC sees that the model is an IEnumerable<Vehicle>
    2. ASP.NET MVC starts enumerating over this collection
    3. For each element of the collection ASP.NET MVC looks at its concrete runtime type.
    4. ASP.NET MVC looks for a corresponding display template for this type defined in the ~/Views/Shared/DisplayTemplates folder and automatically renders it by passing this template the current element as model.

    You will notice something interesting in this pattern: I use the word ASP.NET MVC very much instead of the word the .NET developer. That’s nifty because all that the developer has to do is follow the standard conventions and then ASP.NET MVC will do the job for him so that this developer doesn’t need to write loops, doesn’t need to write if conditions and doesn’t need to reinvent wheels.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.