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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:51:36+00:00 2026-05-16T01:51:36+00:00

Just working through a tutorial on asp.net/mvc for the music store application. Ref: http://www.asp.net/mvc/tutorials/mvc-music-store-part-3

  • 0

Just working through a tutorial on asp.net/mvc for the music store application.

Ref: http://www.asp.net/mvc/tutorials/mvc-music-store-part-3

In the controller they are passing a list of genres to the view model, I am just a beginner but I feel like it is the viewmodel’s job to present the data in what ever format the view requires.

the tutorial code does this…

public ActionResult Index() 
{     
    // Retrieve list of Genres from database     
    var genres = from genre in storeDB.Genres select genre.Name;       

    // Set up our ViewModel     
    var viewModel = new StoreIndexViewModel()
    {         
        Genres = genres.ToList(),         
        NumberOfGenres = genres.Count()
    }; 

    // Return the view     
    return View(viewModel);
}

What I want to do is pass genres to the viewModel and inside the viewModel create the list as well as set the NumberOfGenres property. The way this is coded the controller has to know more about the view than it needs to.

can someone show me what my viewModel class would look like in order to use the ToList() and Count() methods on the genres property inside my viewModel ?

  • 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-16T01:51:36+00:00Added an answer on May 16, 2026 at 1:51 am

    I’m going to disagree with you. The controller knows nothing about the view, only the model. The view model, IMO, should be a simple container just as it is in the tutorial. It’s the controller’s job to fill up the container with data and pass it off to the view.

    It’s an open question whether the view model is as simple as it needs to be, i.e. you can easily derive the number of genres from the list of them so it’s not really necessary to have it as a separate property. If you wanted to just have the list stored in the model, all you would have to do is invoke the Count() method in the view instead of doing it in the controller.

    Not knowing the tutorial, I’m not sure whether they’ve done it this way in anticipation of adding paging to the model, however. If you did want to support paging in the model, then you would want the total count as a separate property since you’d only be passing part of the collection to the view.

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

Sidebar

Related Questions

No related questions found

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.