开发者

how to implement a partial view with generic type inference

开发者 https://www.devze.com 2023-01-28 07:52 出处:网络
I am working through this sample and came across a problem. To implement pagination, the sample extends list and adds pagination to it. This list is th开发者_开发知识库en used as the model.

I am working through this sample and came across a problem. To implement pagination, the sample extends list and adds pagination to it. This list is th开发者_开发知识库en used as the model.

In my view I want to add a pagination control. In the sample they simply add it to the page but I want to make it a user control because I plan to implement pagination in multiple pages. Off course This has to be a strongly typed view but since I can't use wildcards in C# I can not implement it like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PaginatedList<?>>" %>

Since I only plan to use the members declared in PaginatedList and not in List I don't need the type.

In a C# method we could solve this problem with type inference but how is it don in a partial view?


Define an interface containing the pagination properties that you want to use in your partial view. Have your PaginatedList<T> class implement this interface. Have your partial view be typed to the interface.

public interface IPaginated
{
    int PageIndex  { get; }
    int PageSize   { get; }
    int TotalCount { get; }
    int TotalPages { get; }
}

public class PaginatedList<T> : List<T>, IPaginated
{
   ... should not need to change ...
}

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IPaginated>" %>
0

精彩评论

暂无评论...
验证码 换一张
取 消