System.ArgumentNullException:值不能为 null。Jquery 数据表

System.ArgumentNullException: Value cannot be null. Jquery DataTable

提问人:This lowly newb 提问时间:11/15/2019 更新时间:11/18/2019 访问量:750

问:

是的,我知道这是一个非常新手的问题,但我仍然是一个新手

目前我正在学习jquery数据表,按照教程,调试和其他内容中的说明进行操作,并停留在这行代码上

Request.Form.GetValues("draw").FirstOrDefault()

它抛出 HttpRequest.Form 为 null 的错误,我也调试了它,它里面没有任何值

这是我的视图代码

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">

        $('#table').dataTable({
            "ajax": {
                "url": "@{Html.Action("Read", "Roles", new { area = "PM" }); }",
                "contentType": "application/json; charset=utf-8",
                "type": "POST",
                "datatype": "json"
            },
            "processing": true,
            "serverSide": true,
            "orderMulti": false,
            "dom": '<"top"i>rt<"bottom"lp><"clear">', 
            "columns": [
                { "data": "ID", "name": "ID", "autowidth": true },
                { "data": "RoleName", "name": "RoleName", "autowidth": true }
            ]
        });
    $(document).ready(function () {
        $('#grid-src').hide();
    });
</script>

<div class="row" id="grid-src">
    <div class="col-10 offset-1">
        <div class="card card-primary">
            <div class="card-header">
                <h3>Search</h3>
            </div>
            <div class="card-body">
                @{
                    Html.RenderPartial("_Search");
                }
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="form-control">
        <div class="col-12">
            <table id="table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Role Name</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

和 Ajax 方法

[HttpGet]
[Authorize]
public ActionResult Read()
{
    var gridviewControl = new GridviewControl();
    gridviewControl.Draw = Request.Form.GetValues("draw").FirstOrDefault();
    gridviewControl.CurrentIndex = Request.Form.GetValues("start").FirstOrDefault() == null ? 0 :
        Convert.ToInt32(Request.Form.GetValues("start").FirstOrDefault().ToString());
    gridviewControl.PageSize = Request.Form.GetValues("length") == null ? 0 :
        Convert.ToInt32(Request.Form.GetValues("length").ToString());
    gridviewControl.SortColumnName = Request.Form.GetValues
    (
        "columns[" +
        Request.Form.GetValues("[column]").FirstOrDefault().ToString() +
        "][name]"
    ).FirstOrDefault();
    gridviewControl.Direction = string.IsNullOrEmpty(Request.Form.GetValues("order[0][dir]").FirstOrDefault()) ? SortDirections.NONE :
        string.IsNullOrEmpty(Request.Form.GetValues("order[0][dir]").FirstOrDefault()).ToString().ToUpper() == "ASC" ? SortDirections.ASCENDING :
        SortDirections.DESCENDING;

    var mapper = mapConfig.CreateMapper();
    var srcView = new VWPMRoles()
    {
        RoleName = Request.Form.GetValues("columns[1][search]").FirstOrDefault()
    };

    var objSrc = mapper.Map<VWPMRoles, BOPMRoles>(srcView);

    var totalRow = 0;


    var roles = myDataAccessMethod(objSrc, gridviewControl, out totalRow);

    var result = new JsonResult();


    return Json(new
    {
        draw = gridviewControl.Draw,
        recordsFiltered = gridviewControl.CurrentIndex + gridviewControl.PageSize,
        recordsTotal = totalRow,
        data = roles,
        JsonRequestBehavior.AllowGet
    });
}
jQuery ASP.NET-MVC 数据表

评论


答:

-1赞 This lowly newb 11/18/2019 #1

改用 url 操作,新手错误