在MVC Controller中使用多个参数时,Ajax表单序列化不会绑定

我的模型有多个输入的视图(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); }); }; 

“测试”:数字确实正确到控制器中的方法,但模型现在突然变为空?

我究竟做错了什么?

使用.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; 

你可以这样做:

 function callMethod() { var number = 2; var sd = $('#Form').serializeArray(); sd.push({ name:"test", value:number }); $.ajax({ type: "POST", data: sd, url: '@Url.Action("formMethod", "Test")', }).done(function (newTable) { $('#RefundTableDiv').html(newTable); }); };