I have several methods in one of my controllers that does this:
ViewData["Customers"] = LoadCustomers();
ViewData["Employees"] = LoadEmployees();
ViewData["Statuses"] = LoadStatuses();
etc......
Here is LoadCustomers(), but LoadEmployees, LoadStatuses and all the others are virtually the exact same logic:
private static SelectList LoadCustomers()
{
IList<Customer> customers;
try
{
IServiceCallService scService = new ServiceCallService();
customers = scService.GetCustomers();
Customer c = new Customer
{
ID = "",
Name = "-- Select a Facility --"
};
customers.Insert(0, c);
}
catch
{
customers = new List<Customer>();
Customer c = new Customer
{
ID = "",
Name = "-- No Facilities on File --"
};
customers.Insert(0, c);
}
return new SelectList(customers, "ID", "Name");
}
How can I write this code better so I don't need a new method everytime I add a new select list?