【问题标题】:Razor Pages Javascript Ajax not passing parameters to c#Razor Pages Javascript Ajax没有将参数传递给c#
【发布时间】:2020-05-10 21:21:27
【问题描述】:

我有一个 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(filelists))所以我知道这是可行的

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

【问题讨论】:

  • 没有完全阅读您的代码,我觉得罪魁祸首是参数名称'x'。您能否展示您发送到 C# 代码的值“filelists”的外观?如果没有进一步的配置,命名需要匹配,所以你可能需要重命名你的参数。
  • 嗨@Sossenbinder,我尝试将x更改为文件列表,但不幸的是它仍然为空
  • 您是否设置了 AntiForgery 令牌,请参阅stackoverflow.com/questions/46410716/…

标签: javascript c# ajax razor-pages


【解决方案1】:

URL '?handler=Test' 发送请求,如 https://localhost:44389/?handler=Test&amp;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 对象,则应该为 反序列化 数据创建类。

【讨论】:

  • 嗨,我不认为这是我的问题的解决方案。为了澄清,我试图在单击按钮时调用页面上的 c# 函数,将一些数据传递给它。我可以让它通过我的 AJAX 调用 c# 函数,但它不会传入数据。
  • 你能检查我的更新吗?旧的解决方案甚至对我也有效
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 2018-12-14
  • 2012-01-15
相关资源
最近更新 更多