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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:15:02+00:00 2026-05-25T01:15:02+00:00

I have been playing around with MVC 3 and looking at populating dropdownlists. I

  • 0

I have been playing around with MVC 3 and looking at populating dropdownlists. I have seen a few examples online that recommend using view models, so here is my first attempt. My code seems to work, but can anybody tell me if this is the correct way to do this?

My model :

public class ContactGP
{
    public int TeamID { get; set; }

    [Required(ErrorMessage = "Please select a Team Name")]
    [DataType(DataType.Text)]
    [DisplayName("Team Name")]
    public string TeamName { get; set; }
}

My view model :

public class ContactGPViewModel
{
    public string SelectedTeamID { get; set; }
    public IEnumerable<Team> Teams { get; set; }
}

My controller :

public IEnumerable<Team> PopulateTeamsDropDownList()
    {
        IEnumerable<Team> lstTeams = _Base.DataRepository.GetTeams();

        return lstTeams;
    }

    public ActionResult ContactGP()
    {
        var model = new ContactGPViewModel
        {
            Teams = PopulateTeamsDropDownList()
        };

        return View(model);
    }

And my view :

<p>
    @Html.DropDownListFor(
        x => x.SelectedTeamID, 
        new SelectList(Model.Teams, "TeamID", "TeamName")
    )
</p>
  • 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-25T01:15:03+00:00Added an answer on May 25, 2026 at 1:15 am

    Your code seems correct. You have defined a view model containing the necessary properties your view will require, filled it up in the controller and passed to this strongly typed view.

    I have only a minor remark on the following line inside the PopulateTeamsDropDownList method:

    _Base.DataRepository.GetTeams();
    

    I hope you have abstracted this repository with interfaces (or abstract classes) and used DI in order to inject some concrete implementation into your controller. This will weaken the coupling between your controller and the way data is accessed and to simplify unit testing the different layers of your application in isolation.

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

Sidebar

Related Questions

I have been playing around with the concept of 'module' that some mvc frameworks
I have been playing around with the MVP pattern using winforms for the last
I have been playing around with using graphs to analyze big data. Its been
I have been playing around with a quite complex SQL Statement for a few
I have been playing around with creation of pdf documents for a project that
I have been playing around with a few ways to batch insert a list
I have been playing around with different scripts, I found one that works for
I have been playing around with in app purchases for a few days, everything
I've been playing around with using MySql as the membership provider for asp.net mvc
I have been playing around with some debugging and wrote some C code that

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.