rad*_*byx 7 javascript ajax asp.net-mvc asp.net-mvc-4 asp.net-mvc-5
在MVC中对服务器的AJAX请求中,如何将id列表传递给控制器的动作函数?
我接受使用或不使用Html助手.
我知道MVC的模型绑定器在简单类型方面没有问题,比如int,string和bool.
它是否像我必须在行动中使用和数组?
我不在乎我是否必须使用array或者List即使字符串我int或strings我总能转换它们.我只需要在服务器上使用它们.我的列表ID目前为空.
使用Javascript:
var ids= [1,4,5];
// ajax request with ids..
Run Code Online (Sandbox Code Playgroud)
MVC行动:
public ActionResult ShowComputerPackageBuffer(List<int> ids) // ids are null
{
// build model ect..
return PartialView(model);
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加了我的AJAX请求
$(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")';
};
Run Code Online (Sandbox Code Playgroud)
解决方案://感谢@aherrick评论.我错过了古老的"传统"
$.ajax({
type: "POST",
url: '@Url.Action("ShowComputerPackageBuffer", "Buffer")',
dataType: "json",
traditional: true,
data: {
bufferIds: bufferIds
}
});
Run Code Online (Sandbox Code Playgroud)
ahe*_*ick 14
使用traditional参数并将其设置为true.
$.ajax({
type: "POST",
url: "/URL",
dataType: "json",
traditional: true,
data: {}
});
Run Code Online (Sandbox Code Playgroud)
小智 6
试试这个(我已经检查过了):
$(function () {
var ids = [1, 4, 5];
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: '@Url.Action("YourAction", "YourController")',
data: JSON.stringify( { ids: ids })
}).done(function () {
});
});
Run Code Online (Sandbox Code Playgroud)
你必须确保你的contentType就是application/json你的数据字符串化.
| 归档时间: |
|
| 查看次数: |
32677 次 |
| 最近记录: |