HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

View Model Filling Fields

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fillingmodelviewfields

Problem

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 Team { get; set; }

    [Display(Name = "Application")]
    public IEnumerable 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 listOfTeamsSelect = new List();

    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 listOfApplicationsSelect = new List();

    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?

Solution

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 ToSelectList(IEnumerable applications, int selectedApplicationId)
{
    return applications
            .ToList()
            .Select(p =>
                new SelectListItem 
                { 
                    Value = Convert.ToString(item.ID), 
                    Text = item.Name,
                    Selected = item.ID == selectedApplicationId
                });                 
}

private List ToSelectList(IEnumerable teams)
{
    return teams
            .ToList()
            .Select(p =>
                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);
}

Code Snippets

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<SelectListItem> ToSelectList(IEnumerable<Team> applications, int selectedApplicationId)
{
    return applications
            .ToList()
            .Select(p =>
                new SelectListItem 
                { 
                    Value = Convert.ToString(item.ID), 
                    Text = item.Name,
                    Selected = item.ID == selectedApplicationId
                });                 
}

private List<SelectListItem> ToSelectList(IEnumerable<Team> teams)
{
    return teams
            .ToList()
            .Select(p =>
                new SelectListItem 
                { 
                    Value = Convert.ToString(item.ID), 
                    Text = item.Name,
                    Selected = item.ID == selectedTeamId
                });                 
}
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);
}

Context

StackExchange Code Review Q#49123, answer score: 3

Revisions (0)

No revisions yet.