ASP. NET MVC uses Datatables (2)

Source: Internet
Author: User

Implement paging, sorting, and getting the current page data on the server side

Based on the previous article (DataTables Client implementation)

1. Modify the View page code as follows:

<div class= "Row" > <div class= "col-md-12" > <div class= "Panel panel-primary" id= "List-panel" > <div class= "panel-heading" > 

2. Add the required components on the server side:

A:install-package DATATABLES.MVC5

B:install-package System.Linq.Dynamic

3. Add server-side methods:

Public ActionResult Get ([Modelbinder (typeof (Datatablesbinder))] Idatatablesrequest Requestmodel) {Iquer            yable<asset> query = dbcontext.assets; var totalcount = query.            Count (); #region Filtering if (requestmodel.search.value!=string.                Empty) {var value = RequestModel.Search.Value.Trim (); query = query.                                         Where (p = p.barcode.contains (value) | |                                         P.manufacturer.contains (value) | |                                         P.modelnumber.contains (value) | |            P.building.contains (value)); } var filteredcount = query.            Count ();            #endregion #region sorting var sortedcolumns = RequestModel.Columns.GetSortedColumns (); var orderbystring = string.            Empty; foreach (var column in sortedcolumns) {orderBystring + = orderbystring! = string. Empty?                "," : ""; Orderbystring + = (column. Data) + (column.            SortDirection = = Column.OrderDirection.Ascendant? "ASC": "desc"); } query = query. (orderbystring = = string. Empty?            "Barcode ASC": orderbystring); #endregion//paging query = query. Skip (Requestmodel.start).            Take (requestmodel.length); var data = query. Select (Asset=>new {assetid=asset. AssetID, Barcode=asset. Barcode, Manufacturer=asset. Manufacturer, Modelnumber=asset. ModelNumber, Building=asset. Building, Roomno=asset. Roomno, Quantity=asset. Quantity}).            ToList (); Return Json (New Datatablesresponse (Requestmodel.draw, data, Filteredcount, TotalCount), Jsonrequestbehavior.allowget        ); }

4, run the program, view the results

  

ASP. NET MVC uses Datatables (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.