1

I'm trying to send a dynamic object to an ApiController. Setting my breakpoint on the return null line, I see that the parameters is always null.

The AJAX call:

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

    var product = {
        Name: $('#name2').val(),
        Price: $('#price2').val(),
        Category: $('#category2').val()
    };

    $.ajax({
        type: 'GET',
        url: '/api/filter',
        data: JSON.stringify( product ),
        contentType: 'application/json',

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

});

The Controller:

[HttpGet]
public IEnumerable<Products> GetAllProducts(dynamic parameters)
{
    return null;
}

Any idea what I'm doing wrong here?

6
  • I think there needs to be something in the JSON saying that the object you're passing is named parameters for the automatic stuff to work how you're expecting. Commented Nov 1, 2013 at 19:35
  • Maybe JSON.stringify( { parameters: product } ) would get you something. Commented Nov 1, 2013 at 19:36
  • @TimS. I've tried that too, it's still null. Commented Nov 1, 2013 at 20:29
  • In answer to your other question-in-comment below, a GET request is not assumed to have a body, only URL parameters, so no binding is attempted on the body. Commented Jan 17, 2014 at 15:30
  • @TrueBlueAussie, what do you mean with 'not assumed to have a body'? Commented Jan 17, 2014 at 23:30

1 Answer 1

2

EDIT :- Changed original answer from a GET to a POST.

Assuming the code you posted is in the FilterController, the GetAll method normally does not take parameters and is used to get all the products. If you want to populate your dynamic there you should change it to a POST like so..

$.ajax({
        type: 'POST',
        url: '/api/filter/GetAllProducts,
        data: JSON.stringify( product ),
        contentType: 'application/json',

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

then adorn you controller with the HttpPost attribute

[HttpPost]
public IEnumerable<Products> GetAllProducts(dynamic parameters)
{
    return null;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried your option too, it's still null. You're right about GetAll, but this was just a test to get some data in a dynamic object.
Hmm... that's weird, the post did work. But why doesn't a GET call work?
This code is pretty much the same as what we are using. Put a breakpoint before the ajax call and check JSON.stringify( product ) if you are getting null posted.
my guess would have to do with the Default Model binder in WebAPI, and the differences between GET having parameters in the url and POST having it as part of the body.. If you want to keep is as a GET, you should look into simply returning an IQueryable and using the oData $filter $where etc operators and making it an oData endpoint
Do you happen to have a link (to a tutorial or something) about these oData $filter $where etc?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.