Custom message with actionFailure (Core version 15.4600.0.17)

Hi,

I spent all the day to read your forum message to find a solution to my problem.
It seems to be simple, i want the controler to send an error message if crud operation fail.
I tried all the methods you already answered to other people who had the same problem, but it's not working for me, maybe because i'm using ASP.NET Core and not MVC ?

Here is my view :

<body>
    <ej-grid id="FlatGrid" allow-paging="true" action-Failure="actionFailure" allow-filtering="true" allow-sorting="true" load="onLoad">
        <e-page-settings page-size="4"></e-page-settings>
        <e-filter-settings filter-type="Menu" />
        <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="Normal" show-confirm-dialog="true" show-delete-confirm-dialog="true"></e-edit-settings>
        <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"add","edit","delete","update","cancel", "search" }' />
        <e-datamanager json="(IEnumerable<object>)ViewBag.datasource" update-url="/Admin/UsersListCellEditUpdate" insert-url="/Admin/UsersListCellEditInsert" remove-url="/Admin/UsersListCellEditDelete" adaptor="remoteSaveAdaptor" />
        <e-columns>
            <e-column field="strTitre" header-text=@GetMsg("LIBID_USERS_TITRE") text-align="Left" width="30" edit-type="DropdownEdit" validation-rules='new Dictionary<string, object>() { { "required",true}, { "minlength",3}, { "maxlength",20} }'></e-column>
            <e-column field="strPrenom" header-text=@GetMsg("LIBID_USERS_PRENOM") width="80" validation-rules='new Dictionary<string, object>() { { "required",true}, { "minlength",3} }'></e-column>
            <e-column field="strNom" header-text=@GetMsg("LIBID_USERS_NOM") text-align="Left" width="75" validation-rules='new Dictionary<string, object>() { { "required",true}, { "minlength",3} }'></e-column>
            <e-column field="strLogin" header-text=@GetMsg("LIBID_USERS_LOGIN") is-primary-key="true" text-align="Right" format="{0:C2}" width="75" validation-rules='new Dictionary<string, object>() { { "required",true}, { "email",true} }'></e-column>
        </e-columns>
    </ej-grid>
    <script type="text/javascript">
        function actionFailure(args) {
            var popupObj = $("#FlatGrid").data("ejWaitingPopup");
            popupObj.hide();
            this.cancelEdit();
            var str = "";
            str = $(args.error.responseText).find('i').text();
            alert(str);
            alert(args.error.error.responseText);
            alert(args.error.statusText);
            alert('@(ViewData["strLastErrorMessage"])');
        }
    </script>
</body>


and here is my controler code :

        public ActionResult UsersListCellEditInsert([FromBody]CRUDModel<FFWcfServerReference.CUsers> value)
        {
            ViewBag.strLastErrorMessage = "TEST";
            ViewData["strLastErrorMessage"] = "TEST";
            throw new Exception("TEST");
            //Insert record in database
            return Json(value.Value);
        }

As you see, it looks like the same samples you've already show us. 
The problem is that the alert messages are empty, or undifined, or internal server error when i tried all your samples.

And the viewbag is also empty, because i think that the page is not reloaded.


Can you show me please with a sample but for ASP.NET MVC CORE.

Thx by advance,
Fred (from France).


3 Replies

MS Mani Sankar Durai Syncfusion Team November 23, 2017 12:39 PM UTC

Hi Frédéric, 


Thanks for contacting Syncfusion support. 


We have analyzed your query and based on your requirement we have prepared a sample in ASP.NET Core that can be downloaded from the below link. 


In this sample we have thrown the error message from server side (controller) and shown the error message in dialog box.  This has been achieved using actionFailure event in grid.  


Refer the code example 

<ej-dialog id="ErrorList" title="Error" show-on-init="false"  close="onDialogClose"></ej-dialog> 
    <ej-grid id="FlatGrid" allow-paging="true" action-failure="failure"> 
        <e-filter-settings filter-type="Excel"></e-filter-settings> 
        <e-datamanager url="/Home/DataSource" update-url="/Home/NormalUpdate" insert-url="/Home/NormalInsert" remove-url="/Home/NormalDelete" adaptor="UrlAdaptor"></e-datamanager> 
        ... 
        <e-columns> 
            
        </e-columns> 
    </ej-grid> 
    <script> 
        function failure(args) { 
            var error = $(args.error.responseText).eq(9).text();  //get the error message from server side. 
            str += "<tr><td>" + error + "</td></tr>"; 
            $('#ErrorList').html(""); 
            $('#ErrorList').html("<table>" + str + "</table>"); 
            $('#ErrorList').ejDialog("open"); 
        } 
    </script> 
 
[HomeController.cs] 
public ActionResult NormalUpdate([FromBody]CRUDModel<Order> myObject) 
        { 
             
            ... 
            if(myObject.Value.EmployeeID > 9) 
            throw new Exception("EmployeeID must be above 1000"); 
            ... 
        } 
        public ActionResult NormalInsert([FromBody]CRUDModel<Order> value) 
        { 
            if (value.Value.OrderID > 10000) 
                throw new Exception("OrderID must be above 1000"); 
... 
        } 

From the above code example after the exception thrown from the server side the error has been retrieved using actionFailure event in grid. In the actionFailure event we have set the error message to the dialog html and open the dialog using dialog open method.  

Note: Initially we have set the dialog not visible using show-on-init property as false. 

Refer the documentation link. 


Please let us know if you need further assistance. 


Regards, 
Manisankar Durai. 





FP Frédéric Peyronnin November 23, 2017 01:15 PM UTC

Hi,

Thank's a lot for your answer.

var error = $(args.error.responseText).eq(9).text();

this line is the one I missed !
It works great now.

Thank you very much.



MS Mani Sankar Durai Syncfusion Team November 24, 2017 04:09 AM UTC

Hi Frédéric,  
 
We are happy to hear that your problem has been solved.  
 
Please let us know if you need further assistance. 
 
Regards, 
Manisankar Durai 


Loader.
Up arrow icon