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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:12:10+00:00 2026-05-20T01:12:10+00:00

So lets say I have two models: Thingy and Status. Thingy has a Status

  • 0

So lets say I have two models:
Thingy and Status. Thingy has a Status, and Status has many Thingies. It’s typical “Object and Object type relationship”.

I have a view where I just want the number of thingies in each status. Or basically a list of Status.Name and Status.Thingies.Count. I could do exactly this, but is the “right” thing to do to create a view model in the form:

ThingiesByStatusViewModel
-StatusName
-StatusThingiesCount

and hook it up with something like AutoMapper.

For such a trivial example, it probably doesn’t make much of a difference, but it would help me understand better the proper ‘separation of concerns’.

  • 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-20T01:12:10+00:00Added an answer on May 20, 2026 at 1:12 am

    Should I use a viewmodel here?

    Is this a rhetorical question?

    Your view model would look exactly as you propose and it is perfectly adapted to what you are trying to display here:

    public class ThingiesByStatusViewModel
    {
        public string StatusName { get; set; }
        public int StatusThingiesCount { get; set; }
    }
    

    and then your controller would return an IEnumerable<ThingiesByStatusViewModel>. Then in your view you could simply use a display template:

    @Html.DisplayForModel()
    

    and the corresponding display template (~/Views/Shared/DisplayTemplates/ThingiesByStatusViewModel.cshtml):

    @model AppName.Models.ThingiesByStatusViewModel
    <div>
        <span>StatusName: @Model.StatusName</span>
        <span>Number of thingies: @Model.StatusThingiesCount</span>
    </div>
    

    Now let’s look at the mapping layer. Suppose that we have the following domain:

    public class Thingy
    { }
    
    public class Status
    {
        public string StatusName { get; set; }
        public IEnumerable<Thingy> Thingies { get; set; }
    }
    

    and we have an instance of IEnumerable<Status>.

    The mapping definition could look like this:

    Mapper
        .CreateMap<Status, ThingiesByStatusViewModel>()
        .ForMember(
            dest => dest.StatusThingiesCount,
            opt => opt.MapFrom(src => src.Thingies.Count())
        );
    

    and finally the controller action would simply be:

    public ActionResult Foo()
    {
        IEnumerable<Status> statuses = _repository.GetStatuses();
        IEnumerable<ThingiesByStatusViewModel> statusesVM = Mapper.Map<IEnumerable<Status>, IEnumerable<ThingiesByStatusViewModel>>(statuses);
        return View(statusesVM);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two modals joined with a has many two relationship, lets say
Lets say I have two tables - child and parent with many-to-one relation. What
Lets say we have two objects o1 & o2 defined as System.Object, in my
Lets say I have two Rails models, Users and Lists. Users have_many lists and
Let's say you have two models: Post and Category . Each Post has a
I have two models lets say: class superfields(Model): fieldA = models.FloatField() fieldB = models.FloatField()
Lets say you have a two models with blog posts and comments set up
Let's say I have two models Event and Person . Many people attend an
Let's say I have two models, Classes and People. A Class might have one
Let's say I have two simple models project t.string :title vote t.references :project t.integer

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.