I'm learning C#/MVC 4 by refactoring an old PHP program into ASP format. The program's purpose is to create calendar entries for client appointments, including some basic client info. My most recent challenge was to load static data into multiple @Html.DropDownLists on the page. I was able to accomplish this after hacking through this (How to add static list of items in MVC Html.DropDownList()) and that (How can I get this ASP.NET MVC SelectList to work?) , but I feel like I'm reinventing the wheel...
PORTION OF MODEL: (CommonDataModel.cs)
public class apptCounties{
public IEnumerable<SelectItemList> Counties {get; set;}
public apptCounties()
{
Counties = new[]
{
new SelectListItem{Text="Cook",Value="Cook"},
new SelectListItem{Text="Dupage",Value="Dupage"},
new SelectListItem{Text="Lake",Value="Lake"}
};
}
}
VIEWMODEL: (ScheduleDataViewModel.cs)
public class ScheduleDataViewModel {
public apptClient ClientData {get; set;}
/* ^--This is from another model specific to this app - I'm attempting to use
"CommonDataModel.cs" to hold multiple lists of static data (Counties, Races,
Genders, etc) & then append app-specific data through specialized models.
I plan on expanding functionality later. */
public apptCounties Counties {get; set;}
public ScheduleDataViewModel()
{
ClientData = new apptClient();
Counties = new apptCounties();
}
}
CONTROLLER: (ScheduleController.cs)
public ActionResult Schedule()
{
var model = new ScheduleDataViewModel();
return View(model);
}
PORTION OF VIEW: (Index.cshtml - strongly typed to ScheduleDataViewModel)
@Html.DropDownList("Counties", Model.Counties)
Sorry for any mucky syntax there - my code isn't in front of me! I can verify that at least the general idea above is working when built & tested.
I worry that I'm overcomplicating what ought to be a much simpler procedure. Am I actually on the right track with all the constructor methods here, or can someone suggest a more elegant solution to serving static data lists without the benefit of a database?