Consume jQuery.serializeArray in ASP.NET MVC

后端 未结 2 1852
臣服心动
臣服心动 2021-01-26 20:40

I post $(\'#myForm\').serializeArray() to an ASP.NET MVC (2.0) action. serialized array looks as follows:

filters[0][name]    : gemcolor
filters[0][         


        
2条回答
  •  清酒与你
    2021-01-26 21:12

    I know this is old, but how about something like this - this is what I use generically on my pages to submit via ajax:

    $(function () {
    
        @*-- PostAll--*@
    
        $(".postAll").click(function () {
            var container = $(this).closest(".postGroup");
            var p = {};
    
            container.find("input[type='text'], input[type='radio']:checked, input:checkbox:checked, textarea").each(function (i, e) {
    
                p[$(e).attr("name")] = $(e).val();
    
            });
    
            container.find('select').each(function (i, e) {
    
                p[$(e).attr("name")] = $(e).find('option:checked').val();
    
            });
    
            $.post($(this).data("url"), p, function (data, status) {
                //Do Some Notification
            })
    
        });
    
    
    });
    

    I tend to have html structure as:

    1. Div containing all the form elements
    2. A button with within the div
    3. button with a url attribute with the url

    Something like this:

    Then you can have a normal model to capture what you need. Not sure if this is the best way or anything but I use this a of times.

提交回复
热议问题