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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:10:49+00:00 2026-05-15T08:10:49+00:00

I’ve been looking into view models for mvc and I’m looking for the best

  • 0

I’ve been looking into view models for mvc and I’m looking for the best way to do them. I’ve read loads of different articles but none seem to be clear as the “best way.” So far example I might have a Customer model with the following properties:

  • First Name
  • Last Name
  • Title
  • Location

Where location is a foreign key to a location table in the database.

I want to be able to edit this customer but only the first name, last name and location. I’m not bothered about the title in the edit. So in my view I will need to pass a customer and a selected list.

Now from what I’ve read I have the following options (there’s probably many more).

So my question is basically which is the best one?

1)

Add a select list to the ViewData["Location"] and just create a strongly typed view of customer?

2)

Create a view model where I pass a customer and select list (the data access is done in the controller):

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        Customer = customer;
        Locations = locations;
    }
}

3)

Create a view model where I pass a customer and list of locations and create the select list in the view model.

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation);
    }
}

4)

Pass a customer and repository and do the data access in the view model.

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation);
    }
}

5)

Create the view model with just the properties I need:

public class ViewModelTest
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        FirstName = customer.FirstName;
        LastName = customer.LastName ;
        Locations = locations;
    }
}

6)

Or some other combination of the above or another way.

All opinions welcome.

  • 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-15T08:10:50+00:00Added an answer on May 15, 2026 at 8:10 am

    Here’s what I may suggest: have a view model which reflects the fields of strongly typed view:

    public class SomeViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Location { get; set; }
        public IEnumerable<SelectListItem> PossibleLocations { get; set; }
    }
    

    And in your controller action populate this view model:

    public ActionResult Index()
    {
        var customer = Repository.GetCustomer();
        var locations = Repository.GetLocations();
        var viewModel = new SomeViewModel
        {
            FirstName = customer.FirstName,
            LastName = customer.LastName,
            Location = customer.Location,
            PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location);
        };
        return View(viewModel);
    }
    
    [HttpPost]
    public ActionResult Index(SomeViewModel viewModel)
    {
        // TODO: Handle the form submission
        return View(viewModel);
    }
    

    Of course doing the mapping between the model and the view model manually as shown my example could become quite cumbersome and in this case I would recommend you looking at AutoMapper.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.