我的模型有多个输入 View (Html.TextBoxFor(x => x.attribute) 等。我的 ajax 方法是:
function callMethod() {
$.ajax({
type: "POST",
data: $('#Form').serialize() ,
url: '@Url.Action("formMethod", "Test")',
}).done(function (newTable) {
$('#RefundTableDiv').html(newTable);
});
};
这很完美,模型完美地适应了 formMethod,但是当我更改 formMethod 并添加另一个参数(例如“int test”)时,它不再起作用了。
我的方法是这样的:
function callMethod() {
var number = 2;
$.ajax({
type: "POST",
data: {"model": $('#Form').serialize(),
"test": number},
url: '@Url.Action("formMethod", "Test")',
}).done(function (newTable) {
$('#RefundTableDiv').html(newTable);
});
};
“测试”: Controller 中的方法确实正确输入了数字,但模型现在突然为空?
我做错了什么?
最佳答案
使用 .serialize()
将您的模型序列化为查询字符串(例如 someProperty=someValue&anotherProperty=anotherValue&...
)。要添加额外的名称/值对,您可以手动追加,例如
var data = $('#Form').serialize() + '&test=' + number;
$.ajax({
....
data: data;
或使用 param()方法(如果您要添加多个项目和/或数组,则很有用)
var data = $("#Form").serialize() + '&' + $.param({ test: number }, true);
$.ajax({
....
data: data;
关于c# - 在 MVC Controller 中使用多个参数时,Ajax 表单序列化不绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525744/