CRUD Grid - Return from Update action a BadRequest and display the error message

Hi!

I have a grid where I'm returning a BadRequest with a message if there is some error or enter a validation. In my view, I read in the failure function the message of the bad request and display it.

This works perfectly when Add or Delete any data. But when updating a data, this not works... is any way to make it work?

This is my method from controller:

public ActionResult Update(OrderModel value)
{
try
{
var oOrder = db.Order.Find(value.OrderID);
if (oOrder != null)
{
if (oOrder.Pet.Ingredient.Count == 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Falta asignar los ingredientes de la comida");
if (value.ProductionDate.Date >= value.BaseRecurrencyDate.Date || value.ProductionDate.Date >= value.DeliveryDate.Date) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "La fecha de producción no puede ser mayor a la fecha base de recurrencia/fecha de entrega");

oOrder.Price = value.Price;
oOrder.Paid = value.Paid;
oOrder.ProductionDate = value.ProductionDate;
oOrder.DeliveryPrice = value.DeliveryPrice;
oOrder.DeliveryAddress = value.DeliveryAddress;
oOrder.DeliveryDate = value.DeliveryDate;
oOrder.BaseRecurrencyDate = value.BaseRecurrencyDate;
oOrder.EditedByDate = Utility.ConvertToLocal(DateTime.Now);
oOrder.EditedByUser = User.Identity.Name;
db.SaveChanges();

return Json(value);
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, ex.Message);
}
}

This is the failure method from the view:

function failure(args) {
ej.popups.hideSpinner(document.getElementById('OrderForm'))
if (args.error[0]) {
alert(args.error[0].error.responseText.split("HTTP Error 400.0 - ")[1].split('</h3>')[0]);
} else {
alert(args.error);
}
}

Thanks, regards!

5 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team December 23, 2020 12:25 PM UTC

Hi Daniel, 
 
Greetings from Syncfusion support. 
 
Query: In my view, I read in the failure function the message of the bad request and display it. 
This works perfectly when Add or Delete any data. But when updating a data, this not works... is any way to make it work? 
 
We are tried to reproduce the reported behavior with the given code example but it was unsuccessful at our end. We can get the exception details in the actionFailure event of Grid, when update, insert and delete a record based on the validation. 
 
We have prepared a sample for your reference. You can get it from the below link. 
 
 
Screenshot: Get the exception details when we Update, Insert, and Delete a record 
 
 
Still, if you face the same issue, please share the below details to validate further with this. 
 
  1. Share the script version of Grid packages and DLL version of Syncfusion controls.
  2. Reassure yourself that you have handled the exception correctly in your decision.
  3. Share a simple issue reproducible sample or make the issue in the given sample.
 
Regards, 
Rajapandiyan S


DA Daniel December 29, 2020 06:05 PM UTC

Hi!

From your example is working. I tried to put your code into my solution and still not working. Maybe some configuration of Syncfusion tools are wrong, not sure.

I attach the base solution with your code. Can you take a look if you can find something?

Thanks, regards!

Attachment: FernandaGarciaMVZ_33f991a2.zip


RS Rajapandiyan Settu Syncfusion Team December 30, 2020 12:10 PM UTC

Hi Daniel, 
 
Thanks for your update. 
 
We have run the attached sample and performed the update action in the Grid. While updating the record we get the below error in the actionFailure event. 
 
Error: 
 
 
You can resolve this by using below conditions in the actionFailure event. 
 
 
    function ActionFailure(args) { 
        ej.popups.hideSpinner(document.getElementById('Grid')) 
        if (args[0]) { 
            alert(args[0].error.responseText.split("HTTP Error 400.0 - ")[1].split('</h3>')[0]); 
        } else { 
            alert(args.error[0].error.responseText.split("HTTP Error 400.0 - ")[1].split('</h3>')[0]); 
        } 
    } 
 
 
 
By further validation, we found that the reported behavior happened in the version 18.2.59. This has been fixed in our latest versions. We suggested you to update the Syncfusion DLL to latest version to resolve this. Find the below screenshot and sample for your reference. 
 
 
 
 
Please get back to us if you need further assistance with this. 
  
Regards, 
Rajapandiyan S 


Marked as answer

DA Daniel December 30, 2020 03:55 PM UTC

thank you so much! this works.

Regards!


RS Rajapandiyan Settu Syncfusion Team December 31, 2020 10:14 AM UTC

Hi Daniel, 
 
We are glad that the provided solution resolved your requirement. 
 
Regards, 
Rajapandiyan S 


Loader.
Up arrow icon