400 bad request for post method

1 个月 前
i am getting an error Failed to load resource: the server responded with a status of 400 (),
[HttpPost]
[IgnoreAntiforgeryToken]
public IActionResult UpdateSequences(IEnumerable<UpdateProductSequenceModel> products)
{
    if (!_permissionService.AuthorizeAsync(StandardPermissionProvider.ManageProducts).Result)
        return AccessDeniedView();
    int result = 0;
    if(products!=null && products.Count()>0)
    {
        var productList = _productService.GetProductsByIdsAsync(products.Select(x => x.Id).ToArray()).Result;
        foreach(var product in productList)
        {
            try
            {
                product.SequenceNo = products.FirstOrDefault(x => x.Id == product.Id).SequenceNo;
                _productService.UpdateProductAsync(product).Wait();
                result++;
            }
            catch (Exception ex)
            {
                _notificationService.ErrorNotificationAsync(ex).Wait();
                result--;
            }
        }
      
    }
    if (result > 0)
    {
        return Json("true");
    }
    else
    {
        return Problem();
    }
}



addAntiForgeryToken(products);
$.ajax({
     type: "POST",
     url: "@Url.Action("UpdateSequences", "Product", new { area = "Admin" })",
     contentType: 'application/json; charset=utf-8',
     dataType: 'json',
     data:products,
     success: function (response) {
         alert("Sequence Successfully Updated");
         window.location.reload();
     },
     error: function (jqXHR, textStatus, errorThrown) {
         alert("Sequence Update failed!!");

     }, complete: function (jqXHR, textStatus) {
         if (jqXHR.status === 204) {
             showAlert('nothingSelectedAlert', '@T("Admin.Common.Alert.NothingSelected")');
             return;
         }
         updateTable('#products-grid');
     }

});
1 个月 前
Is your UpdateSequences method added to the existing ProductController at the admin?

In this case, you don't need to define the area at the ajax's URL
So can you change

url: "@Url.Action("UpdateSequences", "Product", new { area = "Admin" })",

to
url: "@Url.Action("UpdateSequences", "Product")",
and see how it goes?

Another thing is that you have used the [IgnoreAntiforgeryToken] attribute at the Action method so I don't think you will need to add addAntiForgeryToken(products); at your Ajax request as well.