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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:52:59+00:00 2026-05-31T13:52:59+00:00

When building a view model in asp.net’s MVC3, where should the code go to

  • 0

When building a view model in asp.net’s MVC3, where should the code go to instantiate the objects of that view model? I am doing it mostly in the controller right now, aside from the code to query the database. Here is an example in code:

View Model:

public class WorkListVM
{
    //list for employees
    [Display(Name = "Select A Employee")]
    [Required]
    public int? EmployeeId { get; set; }
    public GenericSelectList EmployeeList { get; set; }
}

Controller Code:

        //build view model
        var vm = new WorkListVM();

        //build employee list
        vm.EmployeeList = new GenericSelectList(0,"-- Select Employee --");
        var employees = new List<Employee>();
        using (var gr = new GenericRepo<Employee>())
        {
            employees = gr.Get().ToList();
        }
        foreach(var employee in employees)
        {
            var gl = new GenericListItem();
            gl.Id = employee.EmployeeId;
            gl.DisplayFields = employee.FirstName + " " + employee.LastName;
            vm.EmployeeList.Values.Add(gl);
        }

Generic Select List is a simple class to hold the data that goes in the helper @html.dropdownfor‘s SelectList. I build these selectlist’s, and also build similar data configurations for view models inside of the controller code. The controller hosting this code has a total of 109 lines of code, so it is not huge or out of control. However, I am always striving to reduce redundancy and sometimes the code in //build employee list ends up being copy pasted (ugh, i hate copy paste) into other controllers.

Is there a better place to have this code? Should I maybe be using the factory pattern to build the data for these selectlists / other view data objects?

EDIT

Thanks for all your help. Here is what I ended up doing. I ended up making a method inside the generic select list class very similar to the .ToSelectList(…) suggested by Richard and Jesse:

    public class GenericSelectList
{
    public List<GenericListItem> Values { get; set; }
    public int StartValue { get; set; }
    public string Message { get; set; }

    public GenericSelectList(int StartValue = 0, string Message = "select")
    {
        Values = new List<GenericListItem>();
        this.StartValue = StartValue;
        this.Message = Message;
    }

    public void BuildValues<T>(List<T> items, Func<T, int> value, Func<T, string> text) where T : class
    {
        this.Values = items.Select(f => new GenericListItem()
        {
            Id = value(f),
            DisplayFields = text(f)
        }).ToList();
    }
}
  • 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-31T13:53:01+00:00Added an answer on May 31, 2026 at 1:53 pm

    If the business logic that goes into creating a view model is quite complex I generally extract it into a helper method that I can test independently of the controller.

    However, that aside, your creation of the view model is perfectly fine within the controller as it is. As has already been noted, how your generating the select list could be made much simpler (not to mention reusable).

    Here is a ToSelectList extension of IEnumerable along with an example of usage:

    public static List<SelectListItem> ToSelectList<T>( this IEnumerable<T> enumerable, Func<T, string> value, Func<T, string> text, string defaultOption)
    {
        var items = enumerable.Select(f => new SelectListItem()
                                              {
                                                  Text = text(f) ,
                                                  Value = value(f)
                                              }).ToList();
    
        if (!string.IsNullOrEmpty(defaultOption))
        {
                        items.Insert(0, new SelectListItem()
                            {
                                Text = defaultOption,
                                Value = string.Empty
                            });
        }
    
        return items;
    }
    

    Within your view model you could add a property like so:

    IEnumerable<SelectListItem> Employees { get; set; }
    

    Within your controller (i’m assuming that your repo is returning IEnumberable):

    var employees = new IEnumerable<Employee>();
    using (var gr = new GenericRepo<Employee>())
    {
        employees = gr.Get();
    }
    
    vm.Employees = employees.ToSelectList(x=>x.FirstName + " " + x.LastName, x=>x.Id, "-- Select Employee --")
    

    And then to setup your drop down list in the view it would look something like:

    @Html.DropDownListFor(model => model.EmployeeId, Model.employees)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET MVC there is Model, View and Controller. MODEL represents entities which are
I'm building an ASP.NET MVC 3 app and I've got a model that looks
I'm building an application using the Supervising Controller pattern (Model View Presenter) and I
What's the pros and cons of using a Model-View-Controller model in building your application?
As a PHP/Web Developer, I'm a huge fan of MVC (Model-View-Controller). I love building
I'm building an Asp.net MVC 2 application. I have an entity called Team that
I'm building a model class in mvc asp.net and I want to use a
I'm building an asp.net MVC 2 app. I have a list view which lists
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.
Background I'm working on building an ASP.Net MVC 3 application that utilizes: JQueryUI for

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.