【发布时间】:2012-09-16 15:10:09
【问题描述】:
我在使用 ASP.NET MVC3、AJAX 和 JQUERY 时遇到问题。我有以下功能
[HttpPost]
public bool Update(int id, FormCollection collection)
这是我的 jQuery 源代码:
$(document).ready(function () {
$('#btnUpdate').click(function (e) {
// Cancel default action
e.preventDefault();
var formCollection = $('#formId').serialize();
$.ajax({
cache: false,
type: 'POST',
url: '@Url.Action("Action","Controller")',
data: { id: $('#id').val(), collection: formCollection },
success: function (data) {
alert(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error during process: \n' + xhr.responseText);
}
});
});
});
id参数提交成功,但是集合(FormCollection)包含一个数组,{[0]:10000,[1]:collection}。我无法解决问题。当我像这样重新设计解决方案时:
[HttpPost]
public bool Update(FormCollection collection)
$(document).ready(function () {
$('#btnUpdate').click(function (e) {
// Cancel default action
e.preventDefault();
$.ajax({
cache: false,
type: 'POST',
url: '@Url.Action("Action", "Controller")',
data: $('#formId').serialize(),
success: function (data) {
alert(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error during process: \n' + xhr.responseText);
}
});
});
});
一切正常。我在传递 2 参数时做错了什么?
谢谢!!!
【问题讨论】:
-
这种方式不会得到
FormCollection你已经通过了模型类的集合定义在这里。喜欢public bool Update(int id, userProfile collection) -
确保在 IE 中检查这个Is JSON.stringify() supported by IE 8?
标签: asp.net-mvc jquery