ASP.NET MVC Jquery Ajax发布表单序列化?

Ajaxfunction

$(function () { $('form').submit(function () { if ($(this).valid()) { $.ajax({ url: this.action, type: this.method, data: { model: $(this).serialize(), locations: getCheckedLocation(), reports: getCheckedReports() }, beforeSend: function () { }, complete: function () { }, success: function (result) { $('#user_operations_container').html(result); setTimeout(function () { LoadAction('@Url.Action("GetAllUsers", "User")') }, 1000); $("#widgets ul li a").removeClass("link_active"); $("#widgets ul li:first-child a").addClass("link_active"); } }); } return false; }); }); 

在ajax数据属性中使用的函数

 function getCheckedLocation() { var nodes = $('#tt_location').tree('getChecked'); var s = ''; for (var i = 0; i < nodes.length; i++) { if (s != '') s += ','; s += nodes[i].text; } return s; } function getCheckedReports() { var nodes = $('#tt_reports').tree('getChecked'); var s = ''; for (var i = 0; i < nodes.length; i++) { if (s != '') s += ','; s += nodes[i].text; } return s; } 

HTML

 
// there are html helpers for model (dropdownlistfor, textboxfor,...)
// checkbox tree (#tt_location)
// checkbox tree (#tt_reports)

调节器

 [HttpPost] public ActionResult _EditUser(UserViewModel model,string locations,string reports) { // model = null // locations and reports are expected. (not null) } 

为什么模型为空? 当我使用像this = data: $(this).serialize(), ajax数据属性时,它的工作模型不是null。

如何使用其他数据(位置,报告)发布模型。

我希望我能解释一下。 谢谢…

试试这样:

  data:$('this').serialize() + "&locations=" + getCheckedLocation() "&reports=" + getCheckedReports() 

它会工作。

希望能帮助到你