簡體   English   中英

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對象,您應該創建用於反序列化數據的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM