Pass dynamic data to mvc controller with AJAX

Posted by Yustme on Stack Overflow See other posts from Stack Overflow or by Yustme
Published on 2013-10-27T09:14:19Z Indexed on 2013/10/27 9:54 UTC
Read the original article Hit count: 267

Filed under:
|
|
|
|

How can I pass dynamic data with an AJAX call to an MVC Controller?

Controller:

public JsonResult ApplyFilters(dynamic filters){
   return null;
}

The AJAX call:

$(':checkbox').click(function (event) {
    var serviceIds = $('input[type="checkbox"]:checked').map(function () {
        return $(this).val();
    }).toArray();

    //alert(serviceIds);

    $.ajax({
        type: 'GET',
        url: '/home/ApplyFilters',
        data: JSON.stringify({
            name: serviceIds
        }),
        contentType: 'application/json',

        success: function (data) {
            alert("succeeded");
        },
        error: function (err, data) {
            alert("Error " + err.responseText);
        }
    });

    //return false;
});

Ideally would be that the filters would contain the serviceIds as a property

For example like this: filters.ServiceIds. I got another filter for a date range and that one would be added like so: filters.DateRange.

And server side get the filter as a dynamic object in the ApplyFilters()

© Stack Overflow or respective owner

Related posts about c#

Related posts about jQuery