jQuery.POST - pass another parameter with Form.Serialize() - Asp.net MVC 3

后端 未结 3 2164
遇见更好的自我
遇见更好的自我 2021-02-10 11:16

As I am working on Asp.Net MVC Application, in my application I am using jQuery.POST method to submit a form.

e.g.

jQuery.post(\'/Product/Sa         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 12:06

    You could use the following plugin to serialize the form into a JSON object and add another parameters:

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

    like this:

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

提交回复
热议问题