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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:42:19+00:00 2026-06-13T16:42:19+00:00

My view model looks something like this: public class CarViewModel { public CarModel Car

  • 0

My view model looks something like this:

public class CarViewModel {
    public CarModel Car { get; set; }
    public List<CarModel> Cars { get; set; }
}

CarModel looks like:

public class Car() {
    public string Make { get; set; }
    public string Model { get; set; }
    . . .
}

My goal is to only have a single view model that I can use for both my list views and single views. For the list view, I would hydrate the Cars list and use that within the view, while ignoring the single Car entity.

For a details view, it would be the opposite. My controller would hydrate the single Car entity, and the view would use that and ignore the List.

This is partially working, but the model binding is weak.

DRY (don’t repeat yourself) and SOC/SRP (separation of concerns/single responsibility principal) seem to be conflicting here. What’s the best way to handle both a list view plus a details view? I have a feeling the answer is to create duplicate view models, but I don’t want to duplicate code if I can help it.

  • 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-13T16:42:20+00:00Added an answer on June 13, 2026 at 4:42 pm

    It seems you are over thinking this a little bit. Simply have one class called CarViewModel, which would look like this:

    public class CarViewModel() {
        public string Make { get; set; }
        public string Model { get; set; }
        . . .
    }
    

    For your Detail/Edit/Create views, your view would be of type CarViewModel. For your list view, simply pass a list of CarViewModel to the view. Make the model type on the list view IEnumerable<CarViewModel>(). This removes a class from your current suggested path forward and keeps a single View Model responsible for all things Car related.

    EDIT

    The concern is – “What if my list view model needs more information than just a list of Cars”. In this case, it would make sense to create a stand alone view model for the list view as it requires additional information.

    public class ListOfCarsViewModel(){
       public IEnumerable<CarViewModel> Cars {get;set;}
       public string SomeOtherProperty {get;set;}
       public bool SomeFlagProperty {get;set;}
    }
    

    Note that we are not repeating ourselves as we are still passing a list of cars to the view. However, since we need more than the list of cars, this view model needs to have more properties than just a list. There is nothing wrong with having multiple view models if your code/views require that information.

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

Sidebar

Related Questions

If I have a view model that looks something like this: public class Car
I my view model(LIST) looks like this: public class ConversationModel { public int ID
I have a view model that looks like this public class ViewModelRound2 { public
My simplified domain model looks something like this: public abstract class Entity<IdK> { public
I have a model that looks something like this: public class SampleModel { public
Let's say I have a model that looks like this: public class Blog {
I have a a view model that looks like. public class StoreItemViewModel { public
I have a model that looks like this: public class Book { public string
I have a class that roughly looks like this: public class ViewModel { public
I have created an OrderFormViewModel which looks something like public class OrderFormViewModel { public

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.