Are there any good ways to keep my controllers simpler when they have models that depend on a lot of select lists? I try to keep most of my controller actions as simple as possible (hopefully no more than 10 or so lines) but on pages that require a lot of dropdowns my actions usually exceed this:
public class Model
{
public IEnumerable<SelectListItem> AllLocations { get; set; }
public IEnumerable<SelectListItem> TopLocations { get; set; }
public IEnumerable<SelectListItem> AllTemplates { get; set; }
public IEnumerable<SelectListItem> TopTemplates { get; set; }
// ...
}
[HttpGet]
public ActionResult Index(int id)
{
var domain = Repository.Get(id);
var model = Mapper.Map<Domain, ViewModel>(item);
// any way to abstract this type of code?
model.AllLocations = new SelectList(repository.GetAllLocations(), "Value", "Text");
model.TopLocations = new SelectList(repository.GetTopLocations(), "Value", "Text");
model.AllTemplates = new SelectList(repository.GetAllTemplates(), "Value", "Text");
model.TopTemplates = new SelectList(repository.GetTopTemplates(), "Value", "Text");
// etc. etc.
return View(model);
}
[HttpPost]
public ActionResult Index(ViewModel model)
{
// any way to abstract this type of code?
model.AllLocations = new SelectList(repository.GetAllLocations(), "Value", "Text");
model.TopLocations = new SelectList(repository.GetTopLocations(), "Value", "Text");
model.AllTemplates = new SelectList(repository.GetAllTemplates(), "Value", "Text");
model.TopTemplates = new SelectList(repository.GetTopTemplates(), "Value", "Text");
// etc. etc.
return View(model);
}