gpt4 book ai didi

javascript - 将选定的行值发送到 Controller MVC 时出现问题

转载 作者:行者123 更新时间:2023-12-03 05:39:42 24 4
gpt4 key购买 nike

当我单击 id="send"按钮时,我尝试将选定的行发送到 Controller 中。问题是,当我尝试使用所选行值发送其他值(在本例中为数字和字符串)时,所选行值将 null 发送到 Controller ,但 Controller 中的数字和字符串不为 null参数。

这是我的 JavaScript 代码,只要我发送选定的行值,它就可以正常工作:

$('#send').click(function () {
var items = {};
var grid = $('#grid').data('kendoGrid');
var selectedElements = grid.select();

for (var j = 0; j < selectedElements.length; j++) {
var item = grid.dataItem(selectedElements[j]);
items['grid[' + j + '].ParecidoCodigo'] = item.ParecidoCodigo;

}
$.ajax({
url: '@Url.Action("Index", "Busqueda")',
type: "POST",
async: false,
data: items,

success: function (result) {
console.log(result);
}
})
})

这是我的 Controller 方法操作:

public ActionResult Index(MarcaParecido[] grid)
{ ... }

到目前为止一切正常。但是当我尝试发送另一个像这样的值时:

$('#send').click(function () {
var items = {};
var grid = $('#grid').data('kendoGrid');
var selectedElements = grid.select();
var enviarDest = $('#destinatario').val();
var marca = $('#numMarca').val();

for (var j = 0; j < selectedElements.length; j++) {
var item = grid.dataItem(selectedElements[j]);
items['grid[' + j + '].ParecidoCodigo'] = item.ParecidoCodigo;

}
$.ajax({
url: '@Url.Action("Index", "Busqueda")',
type: "POST",
async: false,
data: { items, marcas: marca, destinatario: enviarDest },

success: function (result) {
console.log(result);
}
})
})

选定的行值向我发送 null,但其他值不为 null

现在这是我的 Controller :

public ActionResult Index(MarcaParecido[] grid, string marcas, string destinatario)
{...}

我也尝试过 JSON.stringify 但它不起作用。

最佳答案

如果 items 是您要发送到服务器的键/值对的集合,请向其中添加两个附加参数,然后继续向 items 对象发送 item。 MVC 应该读作“网格”。 items 作为集合中的项目列表(正如您现在所做的那样),并查看您指定的变量中的其他两个参数:

items["marcas"] = marca;
items["destinatario"] = enviarDest;

$.ajax({
.
.
data: items

关于javascript - 将选定的行值发送到 Controller MVC 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com