2016-09-04 3 views
0

Je l'ai utilisé DataTable pour charger des données à partir de SQL Server et affichage au tableau. Mais cela ne fait que charger des données. Référence: https://gyazo.com/a77f5bee9c8f4fda3be8f9d130499bbfNe peut pas faire datatables Pagination avec procédure stockée dans MVC5

Non seulement afficher ce message:

Aucun enregistrement trouvé pour montrer

mais aussi, sélectionner la page et le sélecteur de format de page ne présentent pas

normal: https://gyazo.com/3f6d5193f36b756f752bdd20523d64e0

Je vous espère que les gars peuvent me donner un resservir aimables. Merci beaucoup

HTML:

<table class="table table-striped table-bordered table-hover table-checkable" id="datatable_products"> 
    <thead> 
     <tr role="row" class="heading"> 
     <th width="1%"> 
      <input type="checkbox" class="group-checkable"> 
     </th> 
     <th width="10%"> ID </th> 
     <th width="15%"> Product&nbsp;Name </th> 
     <th width="15%"> Category </th> 
     <th width="10%"> Price </th> 
     <th width="15%"> Promotion&nbsp;Price </th> 
     <th width="10%"> Quantity </th> 
     <th width="15%"> Date&nbsp;Created </th> 
     <th width="10%"> Status </th> 
     <th width="10%"> Actions </th> 
     </tr> 
    </thead> 
    <tbody> 
    </tbody> 
</table> 

procédure stockée SQL:

USE [OnlineShop] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[LoadProduct] 
    @Start  INT=0, 
    @PageLimit INT=10 
AS 
BEGIN 
    SET NOCOUNT ON; 
    SELECT p.ID, p.Name, c.Name Category, p.Price, p.PromotionPrice, (SELECT SUM(Quantity) FROM ProductSizeColor WHERE ProductID = p.ID) as 'SumQuantity', p.CreatedDate, p.[Status] 
    FROM Product p 
    LEFT JOIN ProductCategory c 
    ON p.CategoryID = c.ID 
    ORDER BY ID 
    OFFSET @Start ROW 
    FETCH NEXT @PageLimit ROWS ONLY 
END 

Javascript:

var handleProducts = function() { 
    var grid = new Datatable(); 

    grid.init({ 
     src: $("#datatable_products"), 
     onSuccess: function(grid) { 
      // execute some code after table records loaded 
     }, 
     onError: function(grid) { 
      // execute some code on network or other general error 
     }, 
     loadingMessage: 'Loading...', 
     dataTable: { 
      "lengthMenu": [ 
       [10, 20, 50, 100, 150], 
       [10, 20, 50, 100, 150] // change per page values here 
      ], 
      "pageLength": 10, // default record count per page 
      "ajax": { 
       "url": "LoadProductTest", // ajax source 
      }, 
      "processing": true, 
      "serverSide": true, 
      "columns": [ 
          {"data": "ForCheckbox"}, 
          {"data": "ID"}, 
          {"data": "Name"}, 
          {"data": "Category"}, 
          {"data": "Price"}, 
          {"data": "PromotionPrice"}, 
          {"data": "SumQuantity"}, 
          {"data": "CreatedDate"}, 
          {"data": "DisplayStatus"}, 
          {"data": "ForAction"}, 
      ], 
      "order": [ 
        [1, "asc"] 
       ] // set first column as a default sort by asc 
     } 
    }); 

Controller:

public ActionResult LoadProductTest() 
    { 
     // 
     var draw = Request.Form.GetValues("draw").FirstOrDefault(); 
     var start = Request.Form.GetValues("start").FirstOrDefault(); 
     var length = Request.Form.GetValues("length").FirstOrDefault(); 

     int pageSize = length != null ? Convert.ToInt32(length) : 0; 
     int skip = start != null ? Convert.ToInt32(start) : 0; 

     var model = new ProductDao().LoadProduct(skip, pageSize); 

     int totalRecords = 0; 
     return Json(new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = model } , JsonRequestBehavior.AllowGet); 
    } 

Méthode Produit de charge:

public List<ProductViewModel> LoadProduct(int start, int pageLimit) 
    { 
     object[] sqlParams = { 
      new SqlParameter("@Start", start), 
      new SqlParameter("@PageLimit", pageLimit) 
     }; 
     return _db.Database.SqlQuery<ProductViewModel>("LoadProduct @Start, @PageLimit", sqlParams).ToList(); 
    } 

Répondre

0

J'ai trouvé comment résoudre ce problème. Dans le contrôleur, je l'ai mis totalRecords = 0. Il est faux, il devrait obtenir des enregistrements totaux de la table. , vous pas besoin de changer quelque chose d'autre, mais écrire nouvelle procédure stockée pour obtenir le nombre d'enregistrement.

Controller: changement totalRecords = 0 à

int totalRecords = new ProductDao().CountProduct(); 

CountProduct funtion:

public int CountProduct() 
    { 
     return _db.Database.SqlQuery<int>("CountProduct").SingleOrDefault(); 
    } 

procédure stockée pour compter les enregistrements:

CREATE PROCEDURE CountProduct 
AS 
BEGIN 
    SELECT COUNT(*) FROM Product 
END 
GO 
+0

vous voudrez peut-être un coup d'oeil sur: http: //developmentpassion.blogspot.com/2016/08/grid-view-with-server-side-filtering.html –

+0

@EhsanSajjad Depuis que je ne suis pas bon dans la requête Linq si J'ai utilisé SP. En tout cas, merci pour votre réponse. Référer cette vidéo: https://www.youtube.com/watch?v=oCouA3tuA3o&t=1449s –

+0

vous pouvez utiliser la procédure stockée au lieu de Linq, la partie importante dans ce modèle est fortement de type qui est affiché, de sorte que vous ne pouvez pas pas à lire de la demande –