ASP.NET MVC ajax custom filter and paging

ASP.NET MVC ajax custom filter and paging

olegvolkovolegvolkov Posts: 2Questions: 1Answers: 0

I try to use Datatable in mvc application. I have a custom form to filter values, and load data into table by ajax.

$('#tblProducts').DataTable({
serverSide: true,

            bFilter: false,
            "oLanguage": {
                "sUrl": "/Content/DataTables/languages/Russian.json"
            },
            "ajax": function (data, callback, settings) {
                $.ajax({
                    url: "/Admin/Product/Products",
                    type: "POST",
                    data: $("#searchForm").serializeObject() + "&draw=" + data.draw + "&start=" + data.start + "&length=" + data.length
                });
            }

});

In controller i pass my viewmodel to filter values and paging info.
But draw always = 1, and my table didn't show any content. The json come to client, I can see it in fiddler, but table does not show anything.

Controller
public ActionResult Products(ProductSearchViewModel model, int draw, int start, int length){...}

json
${"draw":1,"iTotalRecords":1048,"iTotalDisplayRecords":10,"aaData":[["1","1001F","GoodWill","Воздушные","16","0,0008","0,87","False"],["2","1002F","GoodWill","Тормозные колодки","16","0,000928","1,505","False"],["3","1003F","GoodWill","Тормозные колодки","10","0,001716","1,595","True"],["4","1004F","GoodWill","Тормозные колодки","16","0,0008","1,88","True"],["5","1007F","GoodWill","Тормозные колодки","13","0,001804","2,46","True"],["6","1008F","GoodWill","Тормозные колодки","10","0,001716","1,6","True"],["7","1010F","GoodWill","Тормозные колодки","16","0,00096","1,63","True"],["8","1012F","GoodWill","Тормозные колодки","16","0,001494","0,023902","True"],["9","1013F","GoodWill","Тормозные колодки","16","0,0008","0,92","True"],["10","1013FX","GoodWill","Тормозные колодки","16","0,0008","0,93","True"]]}

And there is no javascript errors in Chrome console.
What shall I do to display data i table?

Answers

  • tomzntomzn Posts: 29Questions: 9Answers: 2

    Hi,

    Can you paste your controller code please. This link might help :

    http://www.dotnetawesome.com/2015/11/jquery-datatable-server-side-pagination-sorting.html

  • olegvolkovolegvolkov Posts: 2Questions: 1Answers: 0
    edited May 2016

    I saw this link. As I said, I need to use my special model, and a special form for it. But if I create it, I cannot find paging data (draw, start and length) in request. I don't understand why need the controller code if the question is only how to post custom form and default paging info on server, but if you want here it is:
    public ActionResult Products(ProductSearchViewModel model, int draw, int start, int length)
    {

            using (_unitOfWork = _factory.Create())
            {
                var allProducts = _unitOfWork.ProductsRepository.GetProducts(model.CategoryID, model.BrandID, model.ProductStateID, null,
                    model.InPrice, model.Code).Select(ConvertUtils.Converter.Convert<RegularProductDomainModel, RegularProduct>).ToList();
                var products = allProducts.Skip(start*length).Take(length).ToList();
                var pi = typeof (RegularProductDomainModel).GetProperties();
                var list =
                    products.Select(
                        product =>
                            (from propertyInfo in pi
                                where !propertyInfo.Name.Equals("Price")
                                select propertyInfo.GetValue(product, null).ToString()).ToArray()).ToList();
                return Json(new
                {
                    draw,
                    iTotalRecords = allProducts.Count(),
                    iTotalDisplayRecords = products.Count(),
                    aaData = list
                },
       JsonRequestBehavior.AllowGet);
            }
        }
    

    But in this case datatable does not load content.

This discussion has been closed.