数据表nopcommerce 3.80中不存在所需的防伪表单字段“__RequestVerificationToken”

问题描述 投票:0回答:1

我想获取数据并在nopcommerce 3.80中的数据表中显示。当我在那时做它显示错误,即不存在所需的防伪形式字段“__RequestVerificationToken”。我想添加类似控制器和视图形式的同时添加防伪关键字,仍然显示相同的错误。

这是我的表单视图,

<form id="Master-form" name="Master-form">
@Html.AntiForgeryToken()
    <div class="content">
        <div class="form-horizontal">
            <div class="row">
            </div>
            <div id="product-edit" class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                    @Html.RenderBootstrapTabHeader("tab-country", @T("Admin.Master.Country"), true)
                </ul>
                <div class="tab-content">
                    @Html.RenderBootstrapTabContent("tab-country", @TabCountry(), true)                    
                </div>
            </div>
        </div>
    </div>
</form>

这是我的部分观点

<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                @T("Admin.Master.CountryList")
            </div>
            <!-- /.panel-heading -->
            <div class="panel-body">
                <table id="dataGrid" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Country Name</th>
                            <th>Active</th>
                            <th>Edit</th>
                            <th>Delete</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>

<script>
    $(document).ready(function () {
        $('#dataGrid').DataTable({
            "scrollX": true,  // scrolling horizontal
            "bSort": false,
            "bFilter": false,
            "processing": true, // for show progress bar
            "serverSide": true, // for process server side
            "pageLength": 5,
            "lengthMenu": [5, 10, 50, 100, 1000, 10000],

            "ajax": {
                "url": 'AwazMaster/CountryList',
                "type": "POST",
                "datatype": "json",
                "contentType": "application/json; charset=utf-8"
            },

            "columnDefs":
                [{
                    "targets": [0],
                    "visible": false,
                    "searchable": true,
                    "orderable": false
                }
                ],
            "columns": [
                { "data": "Id", "name": "Id", "autoWidth": true },
                { "data": "CountryName", "name": "service", "autoWidth": true },
                { "data": "IsActive", "name": "active", "autoWidth": true },

                {
                    "render": function (data, type, row) { return "<a class='btn btn-info' onclick=Edit('" + row.Id + "');   >Edit</a>"; }
                },
                {
                    "render": function (data, type, row) {
                        return "<a href='#' class='btn btn-danger' onclick=DeleteData('" + row.Id + "'); >Delete</a>";
                    }
                },
            ]
        });
    });    
</script>

这是我的控制器,

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult CountryList()
{
    // Getting all data  
    var dataList = _MasterService.CountryList(
        start: Convert.ToInt32(Request.Form["start"]),
        pageSize: Request.Form["length"].ToString() != null ? Convert.ToInt32(Request.Form["length"].ToString()) : 0,
        sortColumnName: Request.Form["columns[" + Request.Form["order[0][column]"] + "][name]"],
        sortColumnDirection: Request.Form["order[0][dir]"]);

    var data = dataList.Select(x => new
    {
        Id = x.Id,
        CountryName = x.CountryName,
        IsActive = x.IsActive,
    });

    //Returning Json Data
    Response.StatusCode = 200;
    return Json(new { draw = Request.Form["draw"], recordsFiltered = dataList.TotalCount, recordsTotal = dataList.TotalCount, data = data });
}

为了更清楚上面的图像显示完整的错误enter image description here

在浏览器控制台中显示500(内部服务器错误)

c# asp.net-mvc antiforgerytoken nopcommerce-3.80
1个回答
0
投票

您必须通过ajax请求传递__RequestVerificationToken内部生成的form,如下所示:

var token = $('input[name="__RequestVerificationToken"]').val();

"ajax": {
    "url": 'AwazMaster/CountryList',
    "type": "POST",
    "headers": {__RequestVerificationToken : token} // <--- here it is
    "datatype": "json",
    "contentType": "application/json; charset=utf-8"
 },
© www.soinside.com 2019 - 2024. All rights reserved.