2

I have a Web API controller that accepts an Inspection model, i.e.

public class Inspection
{
    public int ID {get; set;}
}

and I have create a POST method on the controller. This works fine and I have tested it with jQuery.

In my javascript page I create an Inspection, i.e.

var inspection = {
    ID: '123456'
};

and then do $.ajax like this:

var p = $.ajax({
    type: 'POST',
    url: 'http://...',
    cache: false,
    data: JSON.stringify(item),
    dataType: 'json',
    success: function(response) {
        //do stuff
    }
});

So my question is: if I have multiple inspections to post, how do I send them all to the controller either in one batch or in single batches?

1
  • Can you post your API controller and its action method? Also, instead of relying on the model binding to a List<Inspection> have you tried using a method that accepts HttpRequestMessage and then parsed the request content? Commented Dec 27, 2012 at 20:46

3 Answers 3

3

POST'ing multiple integers in a single call should be easy to do. A few quick things to check:

  1. In your controller, does your method signature look something like:

    public void Post([FromBody]int[] values) { // code here }
    

    Note the use of the [FromBody] attribute.

  2. In your POST, is the content type header set to "Content-Type: application/json"?

Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to give you an upvote and the accepted answer because part of my problem was that I wasn't setting Content-Type as you suggested.
0

I have used the jQuery.json plugin for this kind of situation in the past.

Here's an example I found on how you can use it to send multiple items in a single AJAX call:

<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.json-2.2.min.js") %>" type="text/javascript"></script>
<script type="text/javascript">
  function savePayment() {
    //Creating some test data
    var lineItems = new Object();
    lineItems.Entrys = new Array();
    lineItems.Entrys[0] = new Object({ Approved: "1.0", Deductible: "1.0", Coinsurance: "1.0", PaymentAmount: "1.0", VisitId: "1.0", VisitChargeId: "1.0" });
    lineItems.Entrys[1] = new Object({ Approved: "2.0", Deductible: "2.0", Coinsurance: "2.0", PaymentAmount: "2.0", VisitId: "2.0", VisitChargeId: "2.0" });


    //Posting them to server with ajax
    $.ajax({
      type: 'POST',
      contentType: 'application/json; charset=utf-8',
      url: '<%=Url.Action("SavePayment", "Home") %>',
      dataType: 'json',
      data: $.toJSON(lineItems),
      success: function(result) {
        if (result) {
          alert('Success');
        }
        else {
          alert('Failure');
        }
      }
    });
  }
</script>

2 Comments

Sounds like a good idea, I will give it a go in the morning. It looks like you're posting back to a 'classic' controller and not a Web API one - am I correct or not?
@markp3rry Yes, this example isn't for Web API. The goal was mainly to show you how you could bundle multiple Inspection objects together and use jQuery.json to bundle them together to send them in a single POST.
0

Hm, Depending on what your server is expecting, something like this might work:

var itemArr = [inspection0, inspection1, inspection2]; // etc...

var p = $.ajax({
    type: 'POST',
    url: 'http://...',
    cache: false,
    data: JSON.stringify(itemArr),
    dataType: 'json',
    success: function(response) {
        //do stuff
    }
}).fail(function(data) {
    // handle failure
});

Or, to send in multiple batches:

var i = 0,
    max = itemArr.length;

for (; i < max; i++) {
    $.ajax({
        type: 'POST',
        url: 'http://...',
        cache: false,
        data: JSON.stringify(itemArr[i]),
        dataType: 'json',
        success: function(response) {
            //do stuff
        }
    }).fail(function(data) {
        // handle failure
    });
}

Also, it's probably a good idea to add a .fail(...) handler, as well.

4 Comments

Just tried this and the controller isn't interpreting the data correctly.
@markp3rry Any luck with my update? (Splitting into multiple HTTP calls.) Could you change the method you have configured to the 'POST' verb to accept IEnumerable<Inspection> or List<Inspection> as the parameter?
@markp3rry I didn't realize this question related to ASP.net. Let me know if my answer doesn't apply.
tried doing pretty much what you suggested. The ajax request is hitting the 'success' part but the List<inspections> in the web service method is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.