Asp.net mvc ASP.NET MVC 5显示Jquery数据表中500条记录中的10行,并进行服务器端处理

Asp.net mvc ASP.NET MVC 5显示Jquery数据表中500条记录中的10行,并进行服务器端处理,asp.net-mvc,Asp.net Mvc,我的Jquery数据表有问题。它只显示数据库中500条记录中的前10行。有人能帮我解决这个问题吗 这是我的代码: 控制器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.UI.WebControls; using WinWin.Models; namespace WinWin.Contr

我的Jquery数据表有问题。它只显示数据库中500条记录中的前10行。有人能帮我解决这个问题吗

这是我的代码:

控制器:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using WinWin.Models;

namespace WinWin.Controllers
{
    public class PostController : Controller
    {  
        [Route("post")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("post/ajaxpost")]
        public JsonResult ajaxpost(DTParameters param)
        {
            try
            {
                using (WintayoEntities db = new WintayoEntities())
                {
                    var source = new List<Post>();

                    foreach (var p in db.ccp_post.ToList())
                    {
                        source.Add(new Post
                        {
                            id = p.post_id,
                            title = p.title,
                            date = p.post_on.ToString("yyyy-MM-dd"),
                            views = p.pageviews,
                            published = (p.is_published == 0) ? "Draft" : "Published",
                            featured = (p.is_featured == 0) ? "No" : "Yes",
                            action = string.Format(@"<a href=""#""><i class=""fa fa-edit""></i></a>&nbsp;<a href=""#""><i class=""fa fa-trash-o""></i></a>")

                        });
                    }

                    List<String> columnSearch = new List<String>();

                    foreach (var col in param.Columns)
                    {
                        columnSearch.Add(col.Search.Value);
                    }

                    List<Post> data = new ResultPost().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, source, columnSearch);

                    int count = new ResultPost().Count(param.Search.Value, data, columnSearch);

                    DTResult<Post> result = new DTResult<Post>
                    {
                        draw = param.Draw,
                        data = data,
                        recordsFiltered = count,
                        recordsTotal = count
                    };

                    return Json(result);
                }
            }
            catch (Exception e)
            {
                return Json(new { error = e.Message });
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用System.Web.UI.WebControl;
使用WinWin.Models;
名称空间WinWin.Controllers
{
公共类后控制器:控制器
{  
[路线(“邮政”)]
公共行动结果索引()
{
返回视图();
}
[路线(“邮政站/ajaxpost”)]
公共JsonResult ajaxpost(DTParameters参数)
{
尝试
{
使用(WintayoEntities db=new WintayoEntities())
{
var source=新列表();
foreach(db.ccp_post.ToList()中的var p)
{
来源.添加(新帖子)
{
id=p.post\U id,
title=p.title,
日期=p.post_on.ToString(“yyyy-MM-dd”),
视图=p.pageviews,
已发布=(p.is_published==0)?“草稿”:“已发布”,
特色=(p.is_特色==0)?“否”:“是”,
action=string.Format(@“”)
});
}
List columnSearch=新建列表();
foreach(参数列中的变量列)
{
columnSearch.Add(col.Search.Value);
}
List data=new ResultPost().GetResult(param.Search.Value、param.SortOrder、param.Start、param.Length、source、columnSearch);
int count=new ResultPost().count(param.Search.Value,data,columnSearch);
DTResult结果=新的DTResult
{
draw=参数draw,
数据=数据,
recordsFiltered=计数,
记录总数=计数
};
返回Json(结果);
}
}
捕获(例外e)
{
返回Json(新的{error=e.Message});
}
}
}
}
数据表视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WinWin.Models
{   
    /// <summary>
    /// A full result, as understood by jQuery DataTables.
    /// </summary>
    /// <typeparam name="T">The data type of each row.</typeparam>
    public class DTResult<T>
    {
        /// <summary>
        /// The draw counter that this object is a response to - from the draw parameter sent as part of the data request.
        /// Note that it is strongly recommended for security reasons that you cast this parameter to an integer, rather than simply echoing back to the client what it sent in the draw parameter, in order to prevent Cross Site Scripting (XSS) attacks.
        /// </summary>
        public int draw { get; set; }

        /// <summary>
        /// Total records, before filtering (i.e. the total number of records in the database)
        /// </summary>
        public int recordsTotal { get; set; }

        /// <summary>
        /// Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned for this page of data).
        /// </summary>
        public int recordsFiltered { get; set; }

        /// <summary>
        /// The data to be displayed in the table.
        /// This is an array of data source objects, one for each row, which will be used by DataTables.
        /// Note that this parameter's name can be changed using the ajaxDT option's dataSrc property.
        /// </summary>
        public List<T> data { get; set; }
    }

    /// <summary>
    /// The additional columns that you can send to jQuery DataTables for automatic processing.
    /// </summary>
    public abstract class DTRow
    {
        /// <summary>
        /// Set the ID property of the dt-tag tr node to this value
        /// </summary>
        public virtual string DT_RowId
        {
            get { return null; }
        }

        /// <summary>
        /// Add this class to the dt-tag tr node
        /// </summary>
        public virtual string DT_RowClass
        {
            get { return null; }
        }

        /// <summary>
        /// Add this data property to the row's dt-tag tr node allowing abstract data to be added to the node, using the HTML5 data-* attributes.
        /// This uses the jQuery data() method to set the data, which can also then be used for later retrieval (for example on a click event).
        /// </summary>
        public virtual object DT_RowData
        {
            get { return null; }
        }
    }

    /// <summary>
    /// The parameters sent by jQuery DataTables in AJAX queries.
    /// </summary>
    public class DTParameters
    {
        /// <summary>
        /// Draw counter.
        /// This is used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTables (Ajax requests are asynchronous and thus can return out of sequence).
        /// This is used as part of the draw return parameter (see below).
        /// </summary>
        public int Draw { get; set; }

        /// <summary>
        /// An array defining all columns in the table.
        /// </summary>
        public DTColumn[] Columns { get; set; }

        /// <summary>
        /// An array defining how many columns are being ordering upon - i.e. if the array length is 1, then a single column sort is being performed, otherwise a multi-column sort is being performed.
        /// </summary>
        public DTOrder[] Order { get; set; }

        /// <summary>
        /// Paging first record indicator.
        /// This is the start point in the current data set (0 index based - i.e. 0 is the first record).
        /// </summary>
        public int Start { get; set; }

        /// <summary>
        /// Number of records that the table can display in the current draw.
        /// It is expected that the number of records returned will be equal to this number, unless the server has fewer records to return.
        /// Note that this can be -1 to indicate that all records should be returned (although that negates any benefits of server-side processing!)
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public DTSearch Search { get; set; }

        /// <summary>
        /// Custom column that is used to further sort on the first Order column.
        /// </summary>
        public string SortOrder
        {
            get
            {
                return Columns != null && Order != null && Order.Length > 0
                    ? (Columns[Order[0].Column].Data + (Order[0].Dir == DTOrderDir.DESC ? " " + Order[0].Dir : string.Empty))
                    : null;
            }
        }

    }

    /// <summary>
    /// A jQuery DataTables column.
    /// </summary>
    public class DTColumn
    {
        /// <summary>
        /// Column's data source, as defined by columns.data.
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// Column's name, as defined by columns.name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Flag to indicate if this column is searchable (true) or not (false). This is controlled by columns.searchable.
        /// </summary>
        public bool Searchable { get; set; }

        /// <summary>
        /// Flag to indicate if this column is orderable (true) or not (false). This is controlled by columns.orderable.
        /// </summary>
        public bool Orderable { get; set; }

        /// <summary>
        /// Specific search value.
        /// </summary>
        public DTSearch Search { get; set; }
    }

    /// <summary>
    /// An order, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTOrder
    {
        /// <summary>
        /// Column to which ordering should be applied.
        /// This is an index reference to the columns array of information that is also submitted to the server.
        /// </summary>
        public int Column { get; set; }

        /// <summary>
        /// Ordering direction for this column.
        /// It will be dt-string asc or dt-string desc to indicate ascending ordering or descending ordering, respectively.
        /// </summary>
        public DTOrderDir Dir { get; set; }
    }

    /// <summary>
    /// Sort orders of jQuery DataTables.
    /// </summary>
    public enum DTOrderDir
    {
        ASC,
        DESC
    }

    /// <summary>
    /// A search, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTSearch
    {
        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// true if the global filter should be treated as a regular expression for advanced searching, false otherwise.
        /// Note that normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.
        /// </summary>
        public bool Regex { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间WinWin.Models
{   
/// 
///正如jQuery DataTables所理解的那样,这是一个完整的结果。
/// 
///每行的数据类型。
公共类DTResult
{
/// 
///此对象响应的绘图计数器-来自作为数据请求一部分发送的绘图参数。
///请注意,出于安全原因,强烈建议您将此参数强制转换为整数,而不是简单地将其在draw参数中发送的内容回显给客户端,以防止跨站点脚本(XSS)攻击。
/// 
公共int draw{get;set;}
/// 
///过滤前的记录总数(即数据库中的记录总数)
/// 
public int recordsTotal{get;set;}
/// 
///过滤后的记录总数(即应用过滤后的记录总数,而不仅仅是此数据页返回的记录数)。
/// 
公共int记录过滤{get;set;}
/// 
///要在表中显示的数据。
///这是一个数据源对象数组,每行一个,将由数据表使用。
///请注意,可以使用ajaxDT选项的dataSrc属性更改此参数的名称。
/// 
公共列表数据{get;set;}
}
/// 
///可以发送到jQuery DataTables进行自动处理的附加列。
/// 
公共抽象类DTRow
{
/// 
///将dt tag tr节点的ID属性设置为此值
/// 
公共虚拟字符串DT_RowId
{
获取{return null;}
}
/// 
///将此类添加到dt tag tr节点
/// 
公共虚拟字符串DT_RowClass
{
获取{return null;}
}
/// 
///将此数据属性添加到行的dt tag tr节点,允许使用HTML5 data-*属性将抽象数据添加到节点。
///这将使用jQuery data()方法设置数据,这些数据也可以用于以后的检索(例如在单击事件上)。
/// 
公共虚拟对象DT_RowData
{
获取{return null;}
}
}
/// 
///AJAX查询中jQuery数据表发送的参数。
/// 
公共类DTP参数
{
/// 
///画柜台。
///DataTables使用它来确保服务器端处理请求的Ajax返回是由DataTables按顺序绘制的(Ajax请求是异步的,因此可能会按顺序返回)。
///该参数用作绘图返回参数的一部分(请参见下文)。
/// 
公共int Draw{get;set;}
/// 
///定义表中所有列的数组。
/// 
公共DTColumn[]列{get;set;}
/// 
///定义要对多少列进行排序的数组-即,如果数组长度为1,则执行单列排序,否则执行多列排序。
/// 
公共DTOrder[]顺序{get;set;}
/// 
///分页第一个记录指示器。
///这是当前数据集中的起点(0基于索引-即0是第一条记录)。
    <div class="contentpanel">
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table mb30 table-class-ajax" data-list="post/ajaxpost">
                    <colgroup>
                        <col style="width: 4%;" />
                        <col style="width: auto;" />
                        <col style="width: 13%;" />
                        <col style="width: 9%;" />
                        <col style="width: 8%;" />
                        <col style="width: 8%;" />
                        <col style="width: 105px;" />
                    </colgroup>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Publish Date</th>
                            <th>Pageviews</th>
                            <th>Featured</th>
                            <th>Published</th>
                            <th></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div><!-- contentpanel -->
<script>
    $(document).ready(function () {
        var oTable = $(".table-class-ajax").DataTable({
            "serverSide": true,
            "ajax": {
                "type": "POST",
                "url": "post/ajaxpost",
                "contentType": 'application/json; charset=utf-8',
                'data': function (data) { return data = JSON.stringify(data); }
            },
            "processing": true,
            "paging": true,
            "pagingType": "full_numbers",
            "deferRender": true,
            "columns": [
               { "data": "id" },
               { "data": "title" },
               { "data": "date" },
               { "data": "views" },
               { "data": "featured" },
               { "data": "published" },
               { "data": "action" }
            ],
            "order": [0, "asc"],
            "info": true,
        });
    });
</script>
//...
"paging": false
//...
    /// <summary>
    /// Number of records that the table can display in the current draw.
    /// It is expected that the number of records returned will be equal to    this number, unless the server has fewer records to return.
    /// Note that this can b**e -1 to** indicate that all records should be returned (although that negates any benefits of server-side processing!)
    /// </summary>
    public int **Length** { get; set; }