I have about 10 very simple tables in database:
create table EmploymentStatus
(
Id int identity(1,1) Primary Key,
Name nvarchar(100) NOT NULL
)
These tables are used for DropDownLists
on registration view, like this:
@Html.DropDownListFor(m => m.EmploymentStatus, Model.EmploymentStatusList, Resource.EmploymentStatus, new { @class = "form form-control" })
DropDownListFor
requires a IEnumerable<SelectListItem> selectList
as a collection.
So, I created a method:
public static List<SelectListItem> ToSelectItemList(IQueryable<EmploymentStatu> statuses)
{
return statuses.Select(m => new SelectListItem
{
Text = m.Name,
Value = m.Id.ToString()
}).ToList();
}
The problem is that I have to create a method for each of ten entities. Is there a good way of writing some generic method, like this:
public static List<SelectListItem> ToSelectItemList(IQueryable<T> collection)
{
return collection.Select(m => new SelectListItem
{
Text = m.Name,
Value = m.Id.ToString()
}).ToList();
}