_PaginationPartialView.cshtml:
@model IPagedListViewModel<object>
<p>PartialTest</p>
Index.cshtml:
@model PagedDocumentList
Html.RenderPartial("_PaginationPartialView", Model);
IPagedListViewModel.cs & PagedDocumentList.cs:
public class PagedDocumentList : IPagedListViewModel<DocumentEntity>
{
public PagedDocumentList()
{
ListOfItems = new List<DocumentEntity>();
}
public int NumberOfPagesAvailable { get; set; }
public int CurrentPageIndex { get; set; }
public int PageSize { get; set; }
public List<DocumentEntity> ListOfItems { get; set; }
}
public interface IPagedListViewModel<T>
{
int NumberOfPagesAvailable { get; set; }
int CurrentPageIndex { get; set; }
int PageSize { get; set; }
List<T> ListOfItems { get; set; }
}
I am attempting to pass a concrete type into the partial view however I am getting the following error:
The model item passed into the dictionary is of type 'PagedDocumentList', but this dictionary requires a model item of type 'IPagedListViewModel`1[System.Object]'.
Since PagedDocumentList
implements IPagedListViewModel
, I expected that I would be able to pass the concrete instance into the partial view and then read the object
properties within the partial view. How can I use a single partial view that accepts an interface of generic type?
Workarounds I don't like:
If I update the partial to consume a model of:
@model IPagedListViewModel<DocumentEntity>
Then the partial renders as expected. But I don't want to Type my partial - I want to reuse it with different types.
I can also change the PagedDocumentList
as follows:
public class PagedDocumentList
{
public PagedDocumentList()
{
ListOfItems = new List<DocumentEntity>();
}
public PaginationDetails PaginationDetails { get; set; }
public List<DocumentEntity> ListOfItems { get; set; }
}
public class PaginationDetails
{
public int NumberOfPagesAvailable { get; set; }
public int CurrentPageIndex { get; set; }
public int PageSize { get; set; }
}
But then the interface would only enforce the existence of a concrete type, instead of serving as a signature in my partial view. I prefer to have the partial view accept a generic interface so I don't need to create a new class "just to make it work". PaginationDetails
isn't really a class - it's a collection of properties that some classes may implement.