Hi,
I am filling an ejs-schedule with a DataManager :
var dm = new DataManager()
{
Url = Url.Action("GetDataSchedule", "Schedule", null, Context.Request.Scheme),
Adaptor = "UrlAdaptor",
CrudUrl = Url.Action("UpdateDataSchedule", "Schedule", null, Context.Request.Scheme),
CrossDomain = true
};
<ejs-schedule
actionFailure="OnActionFailure"
>
<e-schedule-eventsettings dataSource="dm">
</e-schedule-eventsettings>
</ejs-schedule>
I would like to return an error message from the UpdateDataSchedule action, and display it in javascript.
So I tried to use actionFailure="OnActionFailure" and I tried to return any StatusCode from my controller's action like this in C# :
return StatusCode(StatusCodes.XXXXX, "Hey, there is a problem");
The actionFailure handler is called but the Http error is always translated to Http error 500 and there is no way to get a custom message travel from my controller's action UpdateDataSchedule to javascript.
Maybe this is not the right approach. Could you please tell me what is the right solution for handling errors from a DataManager Syncfusion ASP.Net Core ?
Please note that I am using DataManager as a C# class. I am not creating DataManager in javascript like here : https://support.syncfusion.com/kb/article/9996/how-to-catch-the-failures-from-datamanager-request
Thanks,
Nabil
Hi Nabil,
UG: https://ej2.syncfusion.com/aspnetcore/documentation/schedule/data-binding?cs-save-lang=1&cs-lang=razor#handling-failure-actions
You can pass the custom error message from the server side to the client side
in the Scheduler's ActionFailure event as shown in the shared snippet. If we
misunderstood your requirement, kindly share more details about your
requirements.
[HomeController.cs]
|
public JsonResult LoadData(Params param) { Response.StatusCode = 500; return Json(new { Message = "There is a problem in LoadData method" }, JsonRequestBehavior.AllowGet); }
|
[Index.cshtml]
|
@using Syncfusion.EJ2
@{ var dataManager = new DataManager() { Url = http://localhost:54738/Home/LoadData, CrudUrl= http://localhost:54738/Home/UpdateData, Adaptor = "UrlAdaptor", CrossDomain = true }; }
<ejs-schedule id="schedule" width="100%" height="550" selectedDate="new DateTime(2017, 6, 11)" actionFailure="onActionFailure"> <e-schedule-eventsettings dataSource="dataManager"> </e-schedule-eventsettings> </ejs-schedule>
<script type="text/javascript">
function onActionFailure(args) { var scheduleObj = document.getElementById('schedule').ej2_instances[0]; var span = document.createElement('span'); scheduleObj.element.parentNode.insertBefore(span, scheduleObj.element); span.style.color = '#FF0000'; var error = args.error; if (error.error.status === 500) { span.innerHTML = error.error.responseText; // Display the custom error message } else { span.innerHTML = 'Unknown error occurred.'; } } </script> |
Regards,
Venkatesh
Hi,
Thanks a lot for your answer. Can you confirm that this works also for errors occurring during updates ?
My crudurl is defined here :
var dataManager = new DataManager()
{
Url = Url.Action("GetDataSchedule", "Schedule", null, Context.Request.Scheme),
Adaptor = "UrlAdaptor",
CrudUrl = Url.Action("UpdateDataSchedule", "Schedule", null, Context.Request.Scheme),
CrossDomain = true
};
The action UpdateDataSchedule contains the following :
Response.StatusCode = 500;
return Json(new { success = false, Message = "There is an error" });
I can see in the javascript console that the actionFailure callback is called :
The argument contains the following :
As you can see the error message is not propagated. Instead I receive a generic 500 error.
Best regards,
Nabil
Nabil,
Yes, you can apply the same solution to display errors that occur during
updates through the actionFailure
event of the Scheduler. We have modified the UpdateData method in our provided
sample according to your query. Now, the error message is passed along, and
this message can be received at the sample's end. Please refer to the attached
screenshots and try the shared sample. It appears that there might be a
difference between your implementation and the sample we provided, which is
leading to the error message not being propagated as expected. If the issue
remains unresolved or if we have misunderstood your requirements, kindly
provide us with the following information. This would be more helpful to assist
you:
[Index.cshtml]
[Homecontroller.cs]
Hi,
We are using the latest version SF (23.2.7) and resposce object is totally different.
Please help how to read error message from that response.
Best regards,
Lucas
Hi Nabil,
From our DataManager version(v23), we are using fetch request and in our older version until 22.2.12 we are using the EJ2 AJAX library (which uses XMLHttpRequest). However, in our latest version, we have transitioned to using the fetch operation. As a result, you won't find XMLHttpRequest response when using the fetch operation.
To obtain a custom error message from the server side within the actionFailure event argument during a fetch operation, you need to use the text() method.
[Index.cshtml]
|
function actionFailure(args) { args.error.error.text().then(text => { console.log(text); }) } |
Regards,
Venkatesh