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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:11:42+00:00 2026-06-05T11:11:42+00:00

I have an MVC app where I’m wanting to display a dropdownlist with info

  • 0

I have an MVC app where I’m wanting to display a dropdownlist with info from the database.

The dropdown will display info from database Cars using the table Make which is the make of the car.

So in my view I will have something like:

@model VectorCheck.ViewModels.CarsViewModel
...

@Html.DropDownListFor(modelItem => Model.MakeId, Model.Makes)
...

So somehow I need to get the view model the list of makes.

So I might have some logic to go with this say only cars that are colour Red.

var redCars = _unitOfWork.Cars(x => x.Colour == "Red");

So my question is where is the best practise to put the logic for this query. Should it go in the viewModel or controller.

The way I see it I have two Options.

Option 1: The controller.

public ActionResult Edit(int id)
        {
            var car = _unitOfWork.CarRepository.Get(id);

            var carMakes = _unitOfWork.CarMakeRepository.Where(x => x.Colour == "Red").Select(u => new SelectListItem { Value = u.CarMakeId.ToString(), Text = u.Name });

            return View("Edit", new InsertUpdateCarViewModel(car, carMakes));
        }

ViewModel

public Car Car { get; set; }
public IEnumerable<SelectListItem> CarMakes { get; set; }

InsertUpdateCarViewModel(Car car, IEnumerable<SelectListItem> carMakes)
{
   Car= car;
   CarMakes = carMakes;

}

So in this example I get the carMakes in the controller and give them to the viewModel which is simply a container.

Opon 2: The viewModel

public ActionResult Edit(int id)
        {
            var car = _unitOfWork.CarRepository.Get(id);

            return View("Edit", new InsertUpdateCarViewModel(car));
        }

ViewModel

public Car Car { get; set; }
public IEnumerable<SelectListItem> CarMakes { get; set; }

InsertUpdateCarViewModel(Car car)
{
   Car= car;

   CarMakes = _unitOfWork.CarMakeRepository.Where(x => x.Colour == "Red").Select(u => new SelectListItem { Value = u.CarMakeId.ToString(), Text = u.Name });

}

So in this option I’m putting the logic to get the correct carmakes in the view model. It is more than a container.

So what I’m wanting to know is which of these ways is the correct 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-06-05T11:11:45+00:00Added an answer on June 5, 2026 at 11:11 am

    In the controller. The ViewModel should not be aware of the unit of work you are using. Also, the view model in this case would be a lot more reusable if it didn’t have to rely on the logic x => x.Colour == "Red". Even though this could be moved to the arguments, in general, I believe your models (and therefor views) would be much more reusable taking care of that in the controller.

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

Sidebar

Related Questions

I have an ASP.Net-MVC app using LinqToSql. I have a subcontracts table, a service_lines
I have an MVC app using Entity Framework and a SQL Server 2008 database.
I have a MVC app and using JQuery. I have anchor that I setup
I have an asp.net mvc app, using JQuery UI dialog. I'm trying to submit
I have configured in following way that spring MVC app using Spring 3.1.1.RELEASE web.xml
I have an MVC app I'm writing. There will be the need for multiple
I have an ASP.NET MVC app and am using Forms auth. When going to
I'm looking into moving an existing app to Azure. It will have an MVC
I have a Spring MVC app where I am using a singleton bean that
I have an MVC 3 app that is using jQuery AJAX to fire off

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.