加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – jQuery.POST – 使用Form.Serialize()传递另一个

发布时间:2020-12-16 03:29:25 所属栏目:asp.Net 来源:网络整理
导读:当我在Asp.Net MVC应用程序上工作时,在我的应用程序中,我使用jQuery.POST方法提交表单. 例如 jQuery.post('/Product/Save',jQuery(document.forms[0]).serialize(),function (data) { alert('Product Added Successfully.); }); 在上面的代码片段中,我想传递
当我在Asp.Net MVC应用程序上工作时,在我的应用程序中,我使用jQuery.POST方法提交表单.

例如

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对象并添加其他参数:

$.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.'); 
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读