Several views from my project have the same dropdownlist...
So, in the ViewModel from that view I have :
public IEnumerable Fo
Extension methods to the rescue
public interface ISelectFoo {
IEnumerable FooDdl { get; set; }
}
public class FooModel:ISelectFoo { /* implementation */ }
public static void PopulateFoo(this ISelectFoo data, FooRepository repo)
{
data.FooDdl = repo.GetAll().ToSelectList(x => x.Id, x => x.Name);
}
//controller
var model=new ViewModel();
model.PopulateFoo(repo);
//a wild idea
public static T CreateModel(this FooRepository repo) where T:ISelectFoo,new()
{
var model=new T();
model.FooDdl=repo.GetAll().ToSelectList(x => x.Id, x => x.Name);
return model;
}
//controller
var model=fooRepository.Create();