Razor Pages Javascript Ajax 没有将参数传递给 c#

Razor Pages Javascript Ajax not passing parameters to c#

我有一个 Javascript 函数,它的目的是将一个数组传回我的 C# 代码

我已经得到它来访问 c# 代码,但它从不传递参数

我的Javascript如下

 var errorsubmits = [];
    var filelists = [];
    var nameselect = document.getElementById("username");
    var nameselected = nameselect.options[nameselect.selectedIndex].text;
    if (nameselected.includes("User"))
        errorsubmits.push("User Name");
    var prioritylevel = document.getElementById("priority");
    var priorityselected = prioritylevel.options[prioritylevel.selectedIndex].text;
    if (priorityselected.includes("Priority"))
        errorsubmits.push("Priority");
    var table = document.getElementById("FileTable");
    var counter = 0;
    var filename = "";
    var images = "";
    var envs = "";
    var printmethod = "";
    var encmethod = "";
    var colour = "";
    var commentsforprint = "";
    var commentsforenclose = "";
    for (var i = 1, row; row = table.rows[i]; i++) {
        for (var j = 0, col; col = row.cells[j]; j++) {
            if (counter == 0) {
                if (j == 0)
                    filename = table.rows[i].cells[j].children[0].value;
                if (j == 1)
                    images = table.rows[i].cells[j].children[0].value;
                if (j == 2)
                    envs = table.rows[i].cells[j].children[0].value;
                if (j == 3)
                    printmethod = table.rows[i].cells[j].children[0].value;
                if (j == 4)
                    encmethod = table.rows[i].cells[j].children[0].value;
                if (j == 5)
                    colour = table.rows[i].cells[j].children[0].value;
            }
            else {
                if (j == 1) {
                    if (table.rows[i].cells[j - 1].innerHTML.includes("Print"))
                        commentsforprint = table.rows[i].cells[j].children[0].value;
                    else
                        commentsforenclose = table.rows[i].cells[j].children[0].value;
                }

            }
        }
        if (i % 3 == 0) {
            if (filename == "")
                errorsubmits.push("Filename (row:" + i + ")");
            if (images == "")
                errorsubmits.push("Images (row:" + i + ")");
            if (envs == "")
                errorsubmits.push("Envs (row:" + i + ")");
            if (printmethod.includes("Method"))
                errorsubmits.push("Print Method (row:" + i + ")");
            if (encmethod.includes("Method"))
                errorsubmits.push("Enc Method (row:" + i + ")");
            if (colour.includes("?"))
                errorsubmits.push("Colour (row:" + i + ")");
            // alert(filename + "\n" + images + "\n" + envs + "\n" + printmethod + "\n" + encmethod + "\n" + colour + "\n" + commentsforprint + "\n" + commentsforenclose);
            filelists.push(nameselected + "\t" + priorityselected + "\t" + document.getElementById('Email').textContent + "\t" + filename + "\t" + images + "\t" + envs + "\t" + printmethod + "\t" + encmethod + "\t" + colour + "\t" + commentsforprint + "\t" + commentsforenclose)
            filename = "";
            images = "";
            envs = "";
            printmethod = "";
            encmethod = "";
            colour = "";
            commentsforprint = "";
            commentsforenclose = "";
            counter = 0;
        }
        else {
            counter++;
        }
    }
    if (errorsubmits.length != 0) {
        alert("Cannot submit!\nThe following lines need filling:\n" + errorsubmits.join("\n"));

    }
    else {
        $.ajax({
            url: '?handler=Test',
            contentType: 'application/json',
            data: JSON.stringify(filelists)

        });
    }

我的 C# 代码目前无法正常工作,因为我无法获取数据是这样的

public JsonResult OnGetTest(IEnumerable<object>x)
    {
        return new JsonResult("TEST");

    }

我已经做了一个警报(JSON.stringify(文件列表))所以我知道这有效

(如果可能的话,我想传递原始数组而不是将其字符串化,但我遵循了另一个 SO 建议)

url '?handler=Test' 发送类似 https://localhost:44389/?handler=Test&filelists=%5B%22nameselected%22%2C%22nameselected1%22%5D 的请求,因为它是 HttpGet 请求而不是 POST

根据评论编辑

1 - 函数 JS:

将任意 字符串 压入数组 filelists,并将您的数据更改为 { "filelists": JSON.stringify(filelists) }

var filelists = [];
// push strings here
$.ajax({
   url: '?handler=Test',
   contentType: 'application/json',
   data: { "filelists": JSON.stringify(filelists) }
});

2 - 在服务器端

public JsonResult OnGetTest(string filelists)
{
   IEnumerable<string> files = JsonConvert.DeserializeObject<IEnumerable<string>>(filelists);

   return new JsonResult("TEST");
}

注意,如果你需要在数组中发送javascript对象,你应该为反序列化[=创建类 36=]数据。