如何将AJAX请求中的id列表传递给MVC中的Server [英] How to pass a list of id's in a AJAX request to the Server in MVC
问题描述
在MVC中对服务器的AJAX请求中,如何将id列表传递给控制器的动作函数?
In a AJAX request to the server in MVC, how can I pass a list of id's to the controller's action function?
我接受使用或不使用Html助手。
I accept with or without use of Html helpers.
我知道MVC的模型绑定器没有问题来自简单类型,如 int
, string
和 bool
。
I know MVC's model binder has no problem when it comes to simple types like int
, string
and bool
.
在行动中是否需要使用和数组?
Is it something like I have to use and array instead in the action?
我不在乎如果我必须使用数组
或 List
,即使字符串我 int
或字符串
我总是可以转换它们。我只需要在服务器上使用它们。
我的列表ID目前为空。
I don't care if I have to use an array
or List
and even if the strings I int
or strings
I can always convert them. I just need them on the server.
My List ids gives null at the moment.
Javascript:
Javascript:
var ids= [1,4,5];
// ajax request with ids..
MVC行动:
public ActionResult ShowComputerPackageBuffer(List<int> ids) // ids are null
{
// build model ect..
return PartialView(model);
}
编辑:添加了我的AJAX请求
Added my AJAX request
$(document).ready(function () {
$('#spanComputerPackagesBuffer').on('click', function () {
var ids = $('#divComputerPackagesBuffer').data('buffer');
console.log('bufferIds: ' + bufferIds);
var data = {
ids: ids
};
var url = getUrlShowComputerPackageBuffer();
loadTable(url, "result", data);
});
});
// AJAX's
function loadTable(url, updateTargetId, data) {
var promise = $.ajax({
url: url,
dataType: "html",
data: data
})
.done(function (result) {
$('#' + updateTargetId).html(result);
})
.fail(function (jqXhr, textStatus, errorThrown) {
var errMsg = textStatus.toUpperCase() + ": " + errorThrown + '. Could not load HTML.';
alert(errMsg);
});
};
// URL's
function getUrlShowComputerPackageBuffer() {
return '@Url.Action("ShowComputerPackageBuffer", "Buffer")';
};
解决方案://感谢@aherrick评论。我错过了旧的传统
$.ajax({
type: "POST",
url: '@Url.Action("ShowComputerPackageBuffer", "Buffer")',
dataType: "json",
traditional: true,
data: {
bufferIds: bufferIds
}
});
推荐答案
使用传统
参数并将其设置为 true
。
Use the traditional
parameter and set it to true
.
$.ajax({
type: "POST",
url: "/URL",
dataType: "json",
traditional: true,
data: {}
});
这篇关于如何将AJAX请求中的id列表传递给MVC中的Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!