I built a solution sending a single JSON record using JQuery ajax to my .NET controller which works great and looks like this..
$.ajax({
data: JSON.stringify({'prodId': 1041, 'sizeId': "XS", 'pdtWeight': 2.3}),
contentType: "application/json; charset=utf-8",
type: 'POST',
url: '@Url.Action("AdjustWeight", "ProductBins")',
success: function (data) {
//do something great here
}
});
Here's the code within the controller "ProductBins":"AdjustWeight"
[Route("ProductBins")]
[ApiController]
public class ProductBinsController : Controller
{
private readonly ApplicationDbContext applicationDbContext;
public ProductBinsController(ApplicationDbContext _applicationDbContext)
{
applicationDbContext = _applicationDbContext;
}
[HttpPost]
[Route("AdjustWeight")]
public JsonResult AdjustWeight(AdjustWeightRequest adjustWeightRequest)
{
var AdjWt = (
from pr in applicationDbContext.Products
join pc in applicationDbContext.Prodcolors on pr.ProdId equals pc.ProdId
join pd in applicationDbContext.Proddetails on pc.PclId equals pd.PclId
where pr.ProdId == adjustWeightRequest.ProdId && pd.PdtSize == adjustWeightRequest.SizeId
select pd.PdtId
).ToList();
var prodDetailsToUpdate = applicationDbContext.Proddetails.Where(i => AdjWt.Contains(i.PdtId));
foreach (var item in prodDetailsToUpdate)
{
item.PdtWeight = adjustWeightRequest.PdtWeight;
}
applicationDbContext.SaveChanges();
return Json(true);
}
}
And here's the AdjustWeightRequest class code..
public class AdjustWeightRequest
{
public int ProdId { get; set; }
public string SizeId { get; set; }
public decimal PdtWeight { get; set; }
}
All of that code above works fantastic. Does exactly what I need it to do. However, I may need to send 1 or more JSON records to the controller. In the example above, I'm only sending 1 JSON record works great. For testing purposes, I hard coded the following in my JavaScript function which is sending 2 JSON records...
$.ajax({
data: JSON.stringify([{'prodId': 1041, 'sizeId': "XS", 'pdtWeight': 2.3}, {'prodId': 1041, 'sizeId': "S", 'pdtWeight': 3.2}]),
contentType: "application/json; charset=utf-8",
type: 'POST',
url: '@Url.Action("AdjustWeight", "ProductBins")',
success: function (data) {
//do something great here
}
});
Notice the addition of the square brackets as well as the slight differences in each record (sizeId and pdtWeight). So I assume I'll need to do some foreach looping in the controller to process each JSON record (1 or more) and update the DB accordingly. But, before I get there, I'm getting a Javascript 400 Bad Request error from the controller so my attempts to put breakpoints in the controller class or the request class and debug are not working. It appears the addition of the square brackets are not working as when I make the simple adjustment to my working code (single record) with the square brackets, I get the same 400 Bad Request error.
Just for reference, I'm hard coding the JSON for testing purposes and ease of explaining this here in this post. The final solution will build a JavaScript array and pass that to the JSON.stringify which automatically adds the square brackets. Hope this is enough information for some insight..
Solved... @AussieJoe solution worked great. Here's the updated controller code..
public JsonResult AdjustWeight(AdjustWeightRequest[] adjustWeightRequest)
{
foreach (var item2 in adjustWeightRequest)
{
var AdjWt = (
from pr in applicationDbContext.Products
join pc in applicationDbContext.Prodcolors on pr.ProdId equals pc.ProdId
join pd in applicationDbContext.Proddetails on pc.PclId equals pd.PclId
where pr.ProdId == item2.ProdId && pd.PdtSize == item2.SizeId
select pd.PdtId
).ToList();
var prodDetailsToUpdate = applicationDbContext.Proddetails.Where(i => AdjWt.Contains(i.PdtId));
foreach (var item in prodDetailsToUpdate)
{
item.PdtWeight = item2.PdtWeight;
}
applicationDbContext.SaveChanges();
}
return Json(true);
}