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?