3

I'm trying to implement my own paging for IEnumerable collections. So, I have a class called PagedList. I'm done with the class itself, I just need to write an HtmlHelper extension method that will render the actual pages. This would be the simplest way to go (at least I can think of):

public static MvcHtmlString Paginate<TModel>(this HtmlHelper<TModel>,string url){
      //do the stuff here
      return new MvcHtmlString("Hi");
}

But, is it possible to make this method available ONLY in those views that are strongly typed to PagedList?

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206

2 Answers2

3

While @Ehsan Sajjad's answer is correct, I've found a neater way to accomplish this:

public static MvcHtmlString Paginate<T>(this HtmlHelper<PagedList<T>> helper,string url){
    .......
}

I've tried and it really works. But there might be some other implications that I cannot think of at the moment, so any comments or corrections are welcome.

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
2

You can put constraint on method so that it can only be called on specific types, for Example IEnumerable<PagedList>, PagedList<TModel> or PagedList if you want, it depends on what Type of model you want, and also depends on implementation of your PagedList class:

public static MvcHtmlString Paginate<TModel>(this HtmlHelper<TModel> helper,string url) where TModel : IEnmerable<PagedList> {
  //do the stuff here
  return new MvcHtmlString("Hi");
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160