Is there an easy way to remove the use of Magic Strings when creating a SelectList, like in this example:
@Html.DropDownListFor( model => model.FooValue, new SelectList( Model.FooCollection, "FooId", "FooText", Model.FooValue) )
The magic strings being "FooId" and "FooText"
The rest of the example is defined as follows:
//Foo Class
public class Foo {
public int FooId { get; set; }
public string FooText { get; set; }
}
// Repository
public class MsSqlFooRepository : IFooRepository {
public IEnumerable<Foo> GetFooCollection( ) {
// Some database query
}
}
//View model
public class FooListViewModel {
public string FooValue { get; set; }
public IEnumerable<Foo> FooCollection { get; set; }
}
//Controller
public class FooListController : Controller {
private readonly IFooRepository _fooRepository;
public FooListController() {
_fooRepository = new FooRepository();
}
public ActionResult FooList() {
FooListViewModel fooListViewModel = new FooListViewModel();
FooListViewModel.FooCollection = _fooRepository.GetFooCollection;
return View( FooListViewModel);
}
}