【问题标题】:datatables and MVC数据表和 MVC
【发布时间】:2013-12-21 19:17:48
【问题描述】:

我在使用 datatables jquery 插件在服务器端进行分页时遇到问题。网格底部的分页控件显示

显示 10 个条目中的 1 到 10 个(从 7,253 个条目中过滤)

但不过滤任何记录。如果我单击下一个或上一个链接,则不会将任何 ajax 请求发送回服务器。我希望看到

显示 7253 个条目中的 1 到 10 个。

控制器代码

    public ActionResult GetDataTable(DataTableParamModel param)
    {

        var allhotelscount = db.Businesses.OfType<Hotel>().Count();

        var filteredhotels = db.Businesses.OfType<Hotel>().OrderBy(h => h.Name);


        IEnumerable<DataTableHotel> displayedhotels = from c in filteredhotels

               .Skip(param.iDisplayStart).Take(param.iDisplayLength)
                select new DataTableHotel
                {
                    Id = c.Id,
                    Name = c.Name,
                    CityState = c.PhysicalCity + ", " + c.PhysicalState,
                    PhoneNumber = c.PhoneNumber,
                    PrimaryContact = c.PrimaryContact.FirstName + " " + c.PrimaryContact.LastName,
                    PrimaryContactPhone = c.PrimaryContact.OfficePhone,
                    PrimaryContactEmail = c.PrimaryContact.EmailAddress,
                };


        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = allhotelscount,
            iTotalDisplayRecords = displayedhotels.Count(),
            aaData = displayedhotels
        },
        JsonRequestBehavior.AllowGet);




    }

查看代码:

            <table id="myDataTable" class="display">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>City, State</th>
                        <th>Phone</th>
                        <th>Contact</th>
                        <th>Phone</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table> 

<script type="text/javascript">
$(document).ready(function () {

    $('#myDataTable').dataTable({
        "bServerSide": true,
        "sAjaxSource": "/Hotel/GetDataTable",
        "bProcessing": true,
        "aoColumns": [
                    { "mData": "Id" },
                    { "mData": "Name" },
                    { "mData": "CityState" },
                    { "mData": "PhoneNumber" },
                    { "mData": "PrimaryContact" },
                    { "mData": "PrimaryContactPhone" },
                    { "mData": "PrimaryContactEmail" },
        ]
    });
});

【问题讨论】:

    标签: asp.net-mvc datatables jquery-datatables


    【解决方案1】:

    您向iTotalDisplayRecords 传递了错误的值,请参见以下代码:

    return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = allhotelscount,
                iTotalDisplayRecords = allhotelscount,
                aaData = displayedhotels
            },
            JsonRequestBehavior.AllowGet);
    

    【讨论】:

    • 哇,我看了多久。感谢您了解
    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多