-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5. Sending Multiple Objects in a Request.txt
31 lines (26 loc) · 1.57 KB
/
5. Sending Multiple Objects in a Request.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Sending multiple objects in the JSON string is very simple. We just need to make a slight change to the JSON, like so:
$("button").click(
function () {
var customer = [
{ Id: null, Name: "Scott", EmailAddress: "[email protected]" },
{ Id:null, Name: "Steve", EmailAddress: "[email protected]" },
{ Id:null, Name: "Ben", EmailAddress: "[email protected]" },
{ Id: null, Name: "Amanda", EmailAddress: "[email protected]" }
];
$.ajax({
type: "POST",
data: JSON.stringify(customer),
url: "api/Basic",
contentType: "application/json"
});
});
Note that we put a set of brackets around the object array [], then we put each customer inside of its own curly
braces {}. Finally, for every customer but the last one, we put a comma after it.
Now, make one slight change to the Post() method in the BasicController.cs:
Change the method signature to this:
public HttpResponseMessage Post([FromBody] List<Customer> customers)
All we did there was change "Customer customer" to "List<Customer> customers".
So we're getting a list of customers instead of a single customer now.
Go ahead and run the project and press the button on the webpage after placing a breakpoint
in the controller's Post() method. You will be able to see that the list of customers has 4 objects in it,
and you'll be able to see the data from the JSON.