如何在ASP.Net Core MVC6中实现DataTable

时间:2019-12-27 18:38:54

标签: asp.net-core datatables asp.net-core-3.0

我已将我的应用程序从ASP.Net MVC 5 Web应用程序转换为ASP.Net Core MVC6。在MVC 5中,我成功实现了DataTables服务器端处理。但是在ASP.Net Core中,我不断得到

  

错误404(未找到)

预览浏览器的“网络”会话中的错误,我得到

  

HTTP错误404.15-未找到   请求过滤模块配置为在查询字符串过长的情况下拒绝请求。

下面是我的网格视图

@model _AuditDTGridViewModel
<table class="table table-striped table-bordered table-hover" cellspacing="0" id="datatab">
    <thead id="sortableHeader" class="bg-grey">
        <tr id="columnFilterRow" class="filter" style="">
            @foreach (var c in Model.ColumnOptions)
            {
            <td>
                @if (c.ShowColumnSearchField)
                {
                if (c.ColumnSearchFieldType.ToLower() == "text")
                {
                <input id="filtercolumn_@c.ColumnName" class="input-xsmall" type="text" style="width: 100% !important" />
                }
                }
                else if (c.DisplayName == "Actions")
                {
                @*<button id="filterColumnSearchBtn" class="btn btn-xs btn-outline green" title="Search Columns" style="border-width: 1px!important; margin-bottom: 2px"><i class="fa fa-search"></i>Search</button>*@
                <button id="clearfilterColumnSearchBtn" class="btn btn-xs btn-outline red" title="Reset Filter" style="border-width: 1px!important"><i class="fa fa-times"></i>Clear Filter</button>
                }
            </td>
            }
        </tr>
        <tr id="sortableHeading" class="heading">
            @foreach (var c in Model.ColumnOptions)
            {
            <th>
                @Html.Raw(c.DisplayName)
            </th>

            }
        </tr>
    </thead>

    <tbody></tbody>
</table>
<script src="~/js/jquery.min.js"></script>
<script src="~/js/jquery.dataTables.min.js"></script>
<script src="~/js/dataTables.bootstrap.js"></script>
<script type="text/javascript">
    var oTable = $('#datatab').DataTable({
        "serverSide": true,
        "ajax": {
            "type": "POST",
            "url": '@Url.Action("AjaxListOfAudits", "Client", Model)',
            "contentType": 'application/json; charset=utf-8',
            'data': function (data) {
                return data = JSON.stringify(data);
            }
        },
        "pagingType": "full_numbers",
        "processing": true,
        "paging": true,
        "deferRender": true,
        "columns": @Html.Raw(Model.data),
    "order": [1, "asc"]
    });
</script>

在我的控制器中,我将调用以下操作

public JsonResult AjaxListOfAudits(_AuditDTGridViewModel _AuditDTGridViewModel, DTParameters param)
{
    // Some codes go here
}

这是我的_AuditDTGridViewModel

public class _AuditDTGridViewModel : BaseSearchFilterViewModel
    {
        public _AuditDTGridViewModel()
            : base()
        {
            AllAuditsOrMyAudits = 1;
        }

        public Dictionary<string, string> AuditType { get; set; }
        public int? MembershipId { get; set; }
        public int AllAuditsOrMyAudits { get; set; }
        public int? AuditAction { get; set; }
        public int? AuditCategory { get; set; }
        public string data { get; set; }
        public List<DTColumnOptions> ColumnOptions { get; set; }
    }

当我尝试调试时,未执行操作。如果我从网址操作中删除模型,则会得到

  

错误400-错误的请求

  1. 我在dotnet核心中遇到了“未找到”错误,怎么办?
  2. 如果在dotnet核心中这种在查询中传递模型的方法不正确,该怎么办?

谢谢。

1 个答案:

答案 0 :(得分:0)

  

HTTP错误404.15-找不到请求过滤模块配置为拒绝查询字符串太长的请求。

在您的代码中,我们可以找到您正在使用以下代码段传递Model对象来生成URL。如果超过长度限制,将导致上述错误。

"url": '@Url.Action("AjaxListOfAudits", "Client", Model)'

如果您希望将_AuditDTGridViewModel和服务器端处理参数都传递给您的操作,请参考以下示例代码以将它们从请求正文传递给我们。

在JS客户端上

var oTable = $('#datatab').DataTable({
    "serverSide": true,
    "ajax": {
        "type": "POST",
        "url": '@Url.Action("AjaxListOfAudits", "Client")',
        "contentType": 'application/json; charset=utf-8',
        data: function (d) {
            //console.log(@Json.Serialize(Model));

            var _auditDTGridViewModel = @Json.Serialize(Model);
            var ajaxAuditsModel = {
                "_AuditDTGridViewModel": _auditDTGridViewModel, "DTParameters": d
            };

            //console.log(ajaxAuditsModel);

            return JSON.stringify(ajaxAuditsModel);
        }
    },

    // other options
});

在控制器操作中

public JsonResult AjaxListOfAudits([FromBody] AjaxAuditsModel ajaxAuditsModel)
{

    // Some codes go here

}

AjaxAuditsModel类

public class AjaxAuditsModel
{
    public _AuditDTGridViewModel _AuditDTGridViewModel { get; set; }
    public DTParameters DTParameters { get; set; }
}

测试结果

enter image description here