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<SelectListItem> Team { get; set; }
[Display(Name = "Application")]
public IEnumerable<SelectListItem> 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<SelectListItem> listOfTeamsSelect = new List<SelectListItem>();
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<SelectListItem> listOfApplicationsSelect = new List<SelectListItem>();
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?
1 Answer 1
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<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
});
}
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);
}