We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Send an error message to the user inside the CrudData method

Hello,

I need to return a message to the user to show some info or tell something is wrong inside the CRUD method.

I already asked this inside another thread CRUD operations not working.

Back there, Karthigeyan asked me to try ActionFailure event.

I managed to make it by returning a status 400 bad request, but I think this is not the right way to do it.



And in the Scheduler:



function onActionFailure(args) {

            console.log(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';
            //span.innerHTML = 'Server exception: 404 Not found';
}

How can I return a proper error message and show it to the user without using a BadRequest status?

Thank you very much!

8 Replies

KK Karthigeyan Krishnamurthi Syncfusion Team September 12, 2019 09:08 AM UTC

Hi Gregory 
  
Syncfusion greetigns. 
 
In ActionFailure event, we can retrieve the failure message through args.error[0].error and for the same below sample is prepared. 
 
function onActionFailure(args) {
console.log(args.error[0].error);
alert(args.error[0].error.responseURL + " "+ args.error[0].error.status + ' (' + args.error[0].error.statusText + ")");
}
 
 
Regards, 
Karthi 
 



GP Gregory Perozzo September 12, 2019 01:51 PM UTC

Hello Karthigeyan,

Thank you very much for your help and for the project provided!

Have a nice day!


GP Gregory Perozzo September 12, 2019 07:46 PM UTC

Hello again Karthigeyan,

How can I send a message to the onActionComplete(args) event from the Controller?

I need to send a specific message after the event creation to the user and it has to be sent from the Controller.

Thank you!


HB Hareesh Balasubramanian Syncfusion Team September 13, 2019 01:30 PM UTC

Hi Gregory, 

Thanks for your update. 

Based on your requirement we have prepared a sample, which can be downloaded from the following link, 

Kindly try the above sample, if you have any concerns please revert us back for further assistance. 

Regards, 
Hareesh 



GP Gregory Perozzo September 16, 2019 07:23 PM UTC

Hello Hareesh,

Thank you very much for your answer and sample.

Unfortunately, in the sample, the ViewBag.Message is set before the event creation, in the Index() method.

To use this inside the CRUD method (UpdateData) I must refresh the page, and I didn't want to do that.

I need to return a success message to the user inside the UpdateData (without refreshing the page) method because my API returns a status message that I need to inform the user.

I tried to add a new property 'Message' to the ScheduleEvent object and set my message on this property and then return the object to the scheduler again, but I didn't succeded.

ScheduleEvent class:

public class ScheduleEvent
    {
        public int Id { get; set; }
        public string Subject { get; set; }
        public string Date { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string StartTimezone { get; set; }
        public string EndTimezone { get; set; }
        public bool IsAllDay { get; set; }
        public string Status { get; set; }
        public string Workload { get; set; }
        public string Student { get; set; }
        public string Teacher { get; set; }
        public string Vehicle { get; set; }
        public string Color { get; set; }
        public bool IsReadonly { get; set; }
        public string Message { get; set; }
    }

UpdateData method:



When I catch the return in the onActionComplete(args) method, the 'Message' property comes empty.

Do you have another solution to this? Something like onActionFailure(args) method where I can pass a message to the response?



HB Hareesh Balasubramanian Syncfusion Team September 17, 2019 05:16 PM UTC

Hi Gregory, 

Thanks for your update. 

Based on the requirement, we have modified our previous sample using CustomAdaptor and sample can be downloaded from the below link, 

<ejs-schedule id="schedule" width="100%" created="onCreate" height="550"> 
</ejs-schedule> 

<script type="text/javascript"> 
    function onCreate() { 
        class CustomAdaptor extends ej.data.UrlAdaptor { 
            processResponse() { 
                // calling base class processResponse function 
                var original = super.processResponse.apply(this, arguments); 
                if (arguments[4].url === "Home/UpdateData" && arguments[3].response !== "") { 
                    alert(arguments[3].response); 
               
                return original; 
           
       
        var schObj = document.querySelector('.e-schedule').ej2_instances[0]; 
        schObj.eventSettings.dataSource = new ej.data.DataManager({ 
            url: "/Home/LoadData", crudUrl: "Home/UpdateData", 
            adaptor: new CustomAdaptor(), crossDomain: true 
        }); 
   
</script> 
  
Kindly try the above sample, if you have any concerns please revert us back for further assistance. 

Regards, 
Hareesh 




GP Gregory Perozzo September 18, 2019 06:26 PM UTC

Hareesh,

That's exactly what I need!

Just tested and it worked perfectly!

You are awesome, SyncFusion is awesome, made my day.

Thank you very much for your support and patience!


KK Karthigeyan Krishnamurthi Syncfusion Team September 19, 2019 04:33 AM UTC

 
We are happy that our solution fulfilled your requirement. 
 
Regards, 
Karthi 
 


Loader.
Live Chat Icon For mobile
Up arrow icon