Jul 06, 2016 04:36 PM|mattdouhan@outlook.com|LINK
HI
I am using the below very simple code for a simple insert when a JSON message comes in the body of the message, but what do I need to change in order to accept a JSONarray in the body with multiple entries?
[HttpPut] public void put([FromBody] orp OrderR) { _context.orp.Add(OrderR); _context.SaveChanges(); }
Also I am assuming the JSON array format should be
[ { JSON DAta here }, { next JSON Object here } ]
All-Star
28988 Points
Jul 06, 2016 06:01 PM|Rajneesh Verma|LINK
Try as below
[HttpPut] public void put([FromBody] string[] OrderR) { string seperator = ","; string data = string.Join(seperator,OrderR.ToList<string>()); // other code }
May help
Jul 06, 2016 06:20 PM|mattdouhan@outlook.com|LINK
ok but how do I then convert that data string back to a OrderR object so I can run
_context.orp.Add(data);
and do I loop through it ?
I apologise but the answer is a bit short for me to take on and test it
Jul 06, 2016 06:22 PM|deepalgorithm|LINK
As an example, your json array would look similar to the following:
[{ "OrderName": "ggg", "OrderNumber": "WS-C3650-24PS-S", "Quantity": "1" }, { "OrderName": "asdfasdf", "OrderNumber": "WS-C3650-24PS-S", "Quantity": "3" }]
And you would pass in a list (as an example):
[FromBody]List<OrderR> orders
Your loop would look similar to the following:
foreach(var orp in orders) { _context.orp.Add(orp); }
Jul 06, 2016 06:43 PM|mattdouhan@outlook.com|LINK
this seems to work but how do I then loop through the list because the add code expects a single orp object?
_context.orp.Add(orp);
And right now it says cannot convert from List to orp object
So I guess I need to some how loop through the List Orders ? but I am unsure how
Jul 06, 2016 06:48 PM|mattdouhan@outlook.com|LINK
WOrked it out, now testing with this code
public void put([FromBody] List<orp> orders) { foreach (orp order in orders) { _context.orp.Add(order); _context.SaveChanges(); } }
Will report back how it goes
Jul 06, 2016 06:56 PM|deepalgorithm|LINK
I've edited my answer. Use a simple ForEach loop.
Jul 06, 2016 07:21 PM|deepalgorithm|LINK
Sure.
You you can call SaveChanges outside of the loop. It will persist all changes to the database at once vs. inserting into the database for every item. Also consider wrapping your entity framework code in a transaction.
If you are using EF 6, then you can do something similar to the following:
Jul 06, 2016 07:40 PM|deepalgorithm|LINK
I haven't used EF Core yet so I can't speak to it.
In EF 6, yes the default is that it wraps Insert, Update or Delete operations in a transaction, whenever you execute SaveChanges().
The additional support allows for finer control - if required.
Star
11184 Points
Microsoft
Jul 07, 2016 02:57 AM|Yohann Lu|LINK
Hi mattdouhan@outlook.com,
Glad you found the solution, the following proposal aims to give you more help.
You can also use a single Object that wraps the multiple parameters/entries.
Web API:
public multipleObject PostAlbum(multipleObject request) { var album = request.o1; var userToken = request.o2; return new multipleObject() { o1= album, o2= userToken }; } public class multipleObject { public ob1 o1 { get; set; } public ob2 o2 { get; set; } } public class ob1 { public int LOG_ID { get; set; } } public class ob2 { public int LOG_ID { get; set; } }
Call this method on the client and send it up as JSON.
var o1 = { LOG_ID: "1" } var o2 = { LOG_ID: "1" } $.ajax( { url: "", type: "POST", contentType: "application/json", data: JSON.stringify({ ob1: o1, ob2: o2 }), success: function (result) { alert(result.Result); } });
Best Regards,
Yohann Lu
11 replies
Last post Jul 07, 2016 02:57 AM by Yohann Lu