View Model Filling Fields I'm currently getting into MVC, and I'm working on a simple CRUD application but to make best use of relational database layout I need to use ViewModels so things look prettier on the UI.

I have a ViewModel:

public class CreateAPIViewModel
{
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Team")]
    public int TeamID { get; set; }

    [Display(Name = "Application")]
    public int ApplicationID { get; set; }

    [Display(Name = "Team")]
    public IEnumerable&lt;SelectListItem&gt; Team { get; set; }

    [Display(Name = "Application")]
    public IEnumerable&lt;SelectListItem&gt; Application { get; set; }

    [Display(Name = "Location")]
    public string Location { get; set; }

    [Display(Name = "Keywords")]
    public string Keywords { get; set; }
}


And in my APIController I have this action:

public ActionResult Create()
{
    CreateAPIViewModel viewModel = new CreateAPIViewModel();

    //Fill team - seems a bit long doesn't it?          
    listOfTeams = team.getAllTeams();

    List&lt;SelectListItem&gt; listOfTeamsSelect = new List&lt;SelectListItem&gt;();

    foreach (_Team_Detail item in listOfTeams)
    {
        listOfTeamsSelect.Add(new SelectListItem { Value = Convert.ToString(item.ID), Text = item.Name });
    }

    viewModel.Team = listOfTeamsSelect;

    //Fill application - seems a bit long doesn't it?          
    listOfApplications = application.getAllApplications();

    List&lt;SelectListItem&gt; listOfApplicationsSelect = new List&lt;SelectListItem&gt;();

    foreach (_Application item in listOfApplications)
    {
        listOfApplicationsSelect.Add(new SelectListItem { Value = Convert.ToString(item.ID), Text = item.Name });
    }

    viewModel.Application = listOfApplicationsSelect;

    //Send view model back
    return View(viewModel);
}


Is this the best way to be going about this process for creating a Create page so I can have dropdowns?
If you aren't considering using a mapping solution such as AutoMapper, you could make this code a bit more succient by removing some of the local variables and creating a couple of local methods that are responsible for creating the select lists.

The end solution would look something along the lines of:

public ActionResult Create()
{
    var viewModel = new CreateAPIViewModel
    {
        Team = ToSelectList(team.getAllTeams(),0),
        Application = ToSelectList(application.getAllApplications(),0)
    };

    //Send view model back
    return View(viewModel);
}

private List&lt;SelectListItem&gt; ToSelectList(IEnumerable&lt;Team&gt; applications, int selectedApplicationId)
{
    return applications
            .ToList()
            .Select(p =&gt;
                new SelectListItem 
                { 
                    Value = Convert.ToString(item.ID), 
                    Text = item.Name,
                    Selected = item.ID == selectedApplicationId
                });                 
}

private List&lt;SelectListItem&gt; ToSelectList(IEnumerable&lt;Team&gt; teams)
{
    return teams
            .ToList()
            .Select(p =&gt;
                new SelectListItem 
                { 
                    Value = Convert.ToString(item.ID), 
                    Text = item.Name,
                    Selected = item.ID == selectedTeamId
                });                 
}


This has the added benefit in that you most likely have a Edit() action as well.  You could re-use these methods.

public ActionResult Edit()
{
    var viewModel = new CreateAPIViewModel
    {
        Team = ToSelectList(team.getAllTeams(),team.Id),
        Application = ToSelectList(application.getAllApplications(),application.Id)
    };

    //Send view model back
    return View(viewModel);
}