由于我正在开发 Asp.Net MVC 应用程序,因此在我的应用程序中我使用 jQuery.POST
方法来提交表单。
例如
jQuery.post('/Product/Save', jQuery(document.forms[0]).serialize(),
function (data) { alert('Product Added Successfully.); }
);
在上面的代码片段中,我想传递另一个参数..比方说.. ProductID
.
所以,我的想法是,我想在 jQuery 中传递
,这样我就可以在我的jQuery(document.forms[0]).serialize()
和 ProductID 变量
。 POST 方法 Controller 的操作方法
中获取Form 和 ProductID
。
有人可以告诉我我会这样做吗?
提前致谢。
最佳答案
您可以使用following plugin将表单序列化为 JSON 对象并添加其他参数:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
像这样:
var data = $('form').serializeObject();
data['ProductId'] = '123';
$.post('<%= Url.Action("Save", "Product") %>', data, function (data) {
alert('Product Added Successfully.');
});
关于asp.net-mvc - jQuery.POST - 使用 Form.Serialize() 传递另一个参数 - Asp.net MVC 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4784416/