在MVC4.0中使用分頁實現類別/子類別有哪些更好的選擇?

[英]What are the better options to implement Category/Sub Category with paging in MVC4.0?


I have a requirement, that I have to implement the Paging and the Current Page records. The record are categorized in Category and Sub Category as mentioned below.

我有一個要求,我必須實現Paging和Current Page記錄。該記錄分類為類別和子類別,如下所述。

If you pay attention to the screenshot, I have to show category as well as Sub Category for the current page pragmatically. Right now, this is a static page.

如果您注意截圖,我必須以實際方式顯示當前頁面的類別和子類別。現在,這是一個靜態頁面。

Do we have any inbuilt feature/Nuget that helps to accomplish this thing quickly in MVC4.

我們是否有任何內置功能/ Nuget有助於在MVC4中快速完成此任務。

enter image description here

1 个解决方案

#1


1  

Honestly this isn't as bad as you think it will be to implement. I usually build a input model that looks like this.

老實說,這並不像你認為的那樣糟糕。我通常會構建一個看起來像這樣的輸入模型。

 public class SearchModel {
      public string Query {get;set;}
      public string Sort {get;set;}
      public string Category {get;set;}
      public string SubCategory {get;set;}
      public int Page {get;set;}
      public int Size {get;set;}
      /* Has Properties */
      public bool HasCategory { get { return !string.IsNullOrWhitespace(Category); } }
      // etc...
 }

Then I use a library called PagedList from NuGet, but you could write your own query with whatever ORM or non-ORM you have. Then I write a nice little extension method to check if a category or sub category exists.

然后我使用來自NuGet的名為PagedList的庫,但是您可以使用任何ORM或非ORM編寫自己的查詢。然后我寫了一個很好的小擴展方法來檢查是否存在類別或子類別。

 /* I'm using EF ish code, but plug in your stuff here */
 public ActionResult Index(SearchModel search) {
    var results = Db.Certifications
                    .If(search.HasCategory, q => q.Where(x => x.Category == search.Category)
                    .If(search.HasSubCategory, q => q.Where(x => x.SubCategory == search.SubCategory)
                    .If(search.HasSort, q => q.OrderBy(/* whatever */)
                    .ToPagedList(page: search.Page, size: search.Size);

      return View(result);
 }

Super simple solution and nothing really getting in your way.

超級簡單的解決方案,沒有什么真的妨礙你。

This is a really simple problem to solve, so don't lean on anything too heavy that might constrain you or frustrate you later.

這是一個非常簡單的問題需要解決,所以不要依賴任何可能會限制你的東西,或者以后會讓你感到沮喪。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/18/724d0db057f3ba36976b8157b2eeeba1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com