Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Jquery Ajax post form serialize?

Ajax function

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $.ajax({
                url: this.action,
                type: this.method,
                data: { model: $(this).serialize(), locations: getCheckedLocation(), reports: getCheckedReports() },
                beforeSend: function () {

                },
                complete: function () {

                },
                success: function (result) {
                    $('#user_operations_container').html(result);
                    setTimeout(function () { LoadAction('@Url.Action("GetAllUsers", "User")') }, 1000);
                    $("#widgets ul li a").removeClass("link_active");
                    $("#widgets ul li:first-child a").addClass("link_active");
                }
            });
        }
        return false;
    });
});

functions that are using in ajax data attribute

function getCheckedLocation() {
    var nodes = $('#tt_location').tree('getChecked');
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        if (s != '') s += ',';
        s += nodes[i].text;
    }
    return s;
}

function getCheckedReports() {
    var nodes = $('#tt_reports').tree('getChecked');
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        if (s != '') s += ',';
        s += nodes[i].text;
    }

    return s;
}  

HTML

<div> // there are html helpers for model (dropdownlistfor, textboxfor,...)
</div>
<div> // checkbox tree (#tt_location)
</div>
<div> // checkbox tree (#tt_reports)
</div>

Controller

[HttpPost]
public ActionResult _EditUser(UserViewModel model,string locations,string reports)
{
    // model = null
    // locations and reports are expected. (not null)
}

Question

Why model is null? When I use ajax data attribute like this = data: $(this).serialize(), , It works model is not null.

How can I post model, with additional data (locations,reports).

I hope I can explain. Thanks...

like image 727
AliRıza Adıyahşi Avatar asked Jan 10 '13 09:01

AliRıza Adıyahşi


People also ask

What is form serialize in Ajax?

Definition and Usage The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

What is Ajax in ASP NET MVC?

As you might be knowing, Ajax is a shorthand for Asynchronous JavaScript and XML. The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features.


1 Answers

Try like this:

 data:$('this').serialize() + "&locations=" + getCheckedLocation() "&reports=" + getCheckedReports() 

It will work.

Hope it helps

like image 139
Karthik Chintala Avatar answered Oct 11 '22 13:10

Karthik Chintala