DataTables总是发送GET请求而不是POST

时间:2019-03-01 09:58:03

标签: asp.net-mvc datatables get

我的DataTables不愿意将POST请求发送到我的ASP.NET MVC操作。我试图搜索互联网,但一无所获。 这是代码:

{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class HolidayRequestForm
{
    public int RequestID { get; set; }

    [ValidSimilarRequests(ErrorMessage =
        "A holiday Request for this date range has already been requested")]
    public int EmployeeID { get; set; }

     [ValidSameWeek(ErrorMessage =
        "Holiday Request Must be made on a weekly Period")]
    [DisplayFormat(DataFormatString = "{0:dd/MMM/yy}", ApplyFormatInEditMode = true)]
    public System.DateTime StartDate { get; set; }

    [ValidStartFinishDate(ErrorMessage =
        "Finish Date can not be Greater than Start date.")]
    [DisplayFormat(DataFormatString = "{0:dd/MMM/yy}", ApplyFormatInEditMode = true)]
    public System.DateTime FinishDate { get; set; }

    [Range(0.0001, int.MaxValue, ErrorMessage = "Hours Requested must be greater than zero. ")]
    public decimal HoursTaken { get; set; }
    public string Comments { get; set; }
    public int YearCreated { get; set; }
    public int MonthCreated { get; set; }
    public int DayCreated { get; set; }
    public Nullable<int> YearOfHoliday { get; set; }
    public Nullable<bool> Approved { get; set; }
    public string SubmittedBy { get; set; }
    public string ApprovedBy { get; set; }

    public virtual Employee Employee { get; set; }
}
}

并在服务器上:

            $(document).ready(function () {
            let token = $('input[name="__RequestVerificationToken"]', this).val();

            let table = $('#hit').DataTable({
                "destroy": true                                             
                , "proccessing": true                                       
                , "pagingType": "full_numbers"                              
                , "stateSave": true                                         
                , "serverSide": true                                        
                , "orderCellsTop": true                                     
                , "fixedHeader": true                                       
                , "ajax": {                        
                    "url": @Html.Action("LoadData", "Home"),    
                    "type": "POST"
                    "data": {
                        __RequestVerificationToken: token
                    }
                }
                , "fixedColumns": true                                      
                , "order": [[1, 'asc']]
                ...

和错误消息:

  

在控制器“ Project.Web.Controllers.HomeController”上找不到公共操作方法“ LoadData”。

请问如何解决此问题。

1 个答案:

答案 0 :(得分:2)

最可能的原因是尝试使用GET方法调用控制器动作的@Html.Action()帮助器:

"url": @Html.Action("LoadData", "Home"),

您应该改用@Url.Action(),它会生成URL字符串,而不是直接调用控制器操作:

"url": '@Url.Action("LoadData", "Home")',