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?

Quoter
  • 4,236
  • 13
  • 47
  • 69
  • 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. – Tim S. Nov 01 '13 at 19:35
  • Maybe `JSON.stringify( { parameters: product } )` would get you something. – Tim S. Nov 01 '13 at 19:36
  • @TimS. I've tried that too, it's still null. – Quoter Nov 01 '13 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. – iCollect.it Ltd Jan 17 '14 at 15:30
  • @TrueBlueAussie, what do you mean with 'not assumed to have a body'? – Quoter Jan 17 '14 at 23:30
  • I meant body data. The definition of a get request is to get a resource specified by a URL. body data is not required as part of a request. – iCollect.it Ltd Jan 19 '14 at 21:24

1 Answers1

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;
}
Excommunicated
  • 1,252
  • 8
  • 14
  • 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`. – Quoter Nov 01 '13 at 20:30
  • Hmm... that's weird, the post did work. But why doesn't a GET call work? – Quoter Nov 01 '13 at 21:30
  • 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. – jones6 Nov 01 '13 at 21:30
  • 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 – Excommunicated Nov 01 '13 at 21:44
  • Do you happen to have a link (to a tutorial or something) about these oData $filter $where etc? – Quoter Nov 01 '13 at 23:09
  • Here's a link to a post by Julie Lerman from a recent MSDN Magazine. There are also other links from this article. http://msdn.microsoft.com/en-us/magazine/dn201742.aspx – Excommunicated Nov 03 '13 at 20:00