asp.net-mvc – jQuery.POST – 使用Form.Serialize()传递另一个参数 – Asp.net MVC 3

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – jQuery.POST – 使用Form.Serialize()传递另一个参数 – Asp.net MVC 3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我在Asp.Net MVC应用程序上工作时,在我的应用程序中,我使用jQuery.POST方法提交表单.

例如

@H_502_7@jQuery.post('/Product/Save',jQuery(document.forms[0]).serialize(),function (data) { alert('Product Added Successfully.); } );

在上面的代码片段中,我想传递另一个参数..让我们说.. ProductID.

所以,我的想法是,我想在jQuery.POST方法中传递jQuery(document.forms [0]).serialize()和ProductID变量,因此我可以在我的控制器的action方法获取Form和ProductID.

有人可以让我知道我会这样做吗?

提前致谢.

最佳答案
您可以使用following plugin将表单序列化为JSON对象并添加其他参数:

@H_502_7@$.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; };

像这样:

@H_502_7@var data = $('form').serializeObject(); data['ProductId'] = '123'; $.post('<%= Url.Action("Save","Product") %>',data,function (data) { alert('Product Added Successfully.'); });

猜你在找的jQuery相关文章