我的一个控制器中有几种方法可以做到这一点:
ViewData["Customers"] = LoadCustomers();
ViewData["Employees"] = LoadEmployees();
ViewData["Statuses"] = LoadStatuses();
etc......
这是 LoadCustomers(),但 LoadEmployees、LoadStatuses 和所有其他的逻辑几乎完全相同:
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");
}
如何更好地编写此代码,以便每次添加新选择列表时都不需要新方法?