I have a pattern set up for any viewmodel that uses pagination, and a single view that will be used accross the site.
So given this interface:
public interface IPaginationModel<T> where T : class
{
IPagedList<T> PagedCollection { get; set; }
int ItemsPerPage { get; set; }
int PageIndex { get; set; }
int TotalPages { get; set; }
}
And this model
public class CollectionsViewModel : IPaginationModel<CollectionModel>
{
public IEnumerable<CollectionModel> Collections { get; set; }
public IPagedList<CollectionModel> PagedCollection { get; set; }
public int ItemsPerPage { get; set; }
public int PageIndex { get; set; }
public int TotalPages { get; set; }
public CollectionsViewModel()
{
}
}
I want to use a view/.cshtml in a way like so...
@model Solnet.TeddyPicker.Generator.Models.Pagination.IPaginationModel<T>
@Html.PagedListPager(Model.PagedCollection, page => Url.Action("Index", new { pagesize = Model.ItemsPerPage, page = Model.PageIndex }),
new X.PagedList.Web.Common.PagedListRenderOptions()
{
LiElementClasses = new string[] { "page-item" },
PageClasses = new string[] { "page-link" },
Display = X.PagedList.Web.Common.PagedListDisplayMode.Always
})
But it doesn't like 'T', namespace cannot be found. So is there a way for a view to accept this generic type?
The point is that it makes no sense using generics....if you use generics a lot of operations will not be allowed. You will not be able to use helpers that use lambda
expressions like TextBoxFor.
The right way to proceed when you need to do a View to be used with several types is by using dynamic variable, so you should do something like:
if you do this you will be allowed to do ANY operations on your List, since the actual syntactic check will be done at runtime...however also this solutions has drawbacks:
you are allowed to invoke any method on your helpers or type containing dynamic, but no Intellisense is available since VisualStudio don't know the actual types.
Extension types can't be used, this means you cannot call Html.TextBox(......but you have to call the actual static method that define the Extension Method...like
this: InputExtensions.TextBox(Html,....