Reputation: 21176
Is there a way to template this class so that I can have a generic ListViewModel that can store IEnumerable<Whatever>
. So instead of the following:
public class JamesListViewModel
{
public IEnumerable<James> Jamess { get; set; }
public PagingInfo PagingInfo { get; set; }
}
I would have:
public class ListViewModel
{
public IEnumerable<T> Items { get; set; }
public PagingInfo PagingInfo { get; set; }
}
Otherwise for every list page that has pagination etc, i'm going to have to create a new view model. And more so, If I could get the latter to work. I could inherit and refine if needs be for a specific view.
Upvotes: 2
Views: 92
Reputation: 2476
You need to make the class generic:
public class ListViewModel<T>
{
public IEnumerable<T> Items { get; set; }
public PagingInfo PagingInfo { get; set; }
}
Then, an instance can be created by supplying the type parameter like so:
var vm = new ListViewModel<string>();
Multiple type parameters can be separated by commas:
public class ListViewModel<T, U>
{
public IEnumerable<T> Items { get; set; }
public IEnumerable<U> OtherItems { get; set; }
public PagingInfo PagingInfo { get; set; }
}
var vm = new ListViewModel<string, int>();
See this guide for more information.
Upvotes: 4
Reputation: 109792
Just do this:
public class ListViewModel<T>
{
public IEnumerable<T> Items { get; set; }
public PagingInfo PagingInfo { get; set; }
}
See the MSDN documentation for full details:
http://msdn.microsoft.com/en-us/library/sz6zd40f.aspx
Upvotes: 2