2

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);
}
1
  • 2
    Have you tried public JsonResult AdjustWeight(List<AdjustWeightRequest> adjustWeightRequest)? Commented Mar 16, 2021 at 4:56

1 Answer 1

2

Your controller is setup to only allow one object at a time...but in your 2nd example, you are trying to pass in an array on your stringify call.

Change your API controller's method signature to:

public JsonResult AdjustWeight(AdjustWeightRequest[] adjustWeightRequest)

This will allow an array of objects, instead of a singular object.

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

1 Comment

Brilliant!! That worked beautifully! Thank you soooo much...

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.