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

Error handling when inserting\updating, actionFailure event not firing.

I am trying to show the error returned by the server side ASP.Net core code. i have added a breakpoint in the actionFailure javascript method but the breakpoint is not hit. What am i missing? 

 public async Task<JsonResult> OnPostInsert([FromBody]SyncfusionControlCrudModel<Keyword> syncfusionControlCrudModel)
    {
      var keywordText = syncfusionControlCrudModel?.value?.Value?.ToLowerInvariant();
      if (string.IsNullOrEmpty(keywordText) || keywordText.Length<4)
      {
        ModelState.AddModelError(string.Empty, "The keyword text needs to be at least 3 characters");
        return null;
      }
      var identityUser = await UserManager.GetUserAsync(User);
      var keyword = new Keyword()
      {
        UserId = identityUser.Id,
        Value = keywordText
      };
      ApplicationDbContext.Keywords.Add(keyword);
      await ApplicationDbContext.SaveChangesAsync();
      return new JsonResult(syncfusionControlCrudModel.value);
    }


 <div class="col-md-12">
                                    @Html.AntiForgeryToken()
                                    <div class="form-group">
                                        <label class="form-control-label">New keywords</label>
                                        <ejs-grid id="GridNewKeywords" allowPaging="true" load="onLoad" actionFailure="actionFailure"
                                                  actionbegin="actionbegin" toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})">
                                            <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true" />
                                            <e-data-manager url="/Settings?handler=DataSource" insertUrl="/Settings?handler=Insert"
                                                            updateUrl="/Settings?handler=Update" removeUrl="/Settings?handler=Delete"
                                                            adaptor="UrlAdaptor" />
                                            <e-grid-pageSettings pageCount="5" pageSize="5" />
                                            <e-grid-columns>
                                                <e-grid-column field="KeywordId" headerText="Id" visible="false" isPrimaryKey="true" width="0" allowEditing="false" />
                                                <e-grid-column field="UserId" headerText="UserId" visible="false" isPrimaryKey="true" width="0" allowEditing="false" />
                                                <e-grid-column field="Value" headerText="Keyword" validationRules="@(new { required=true})" />
                                            </e-grid-columns>
                                        </ejs-grid>

                                    </div>
                                </div>


<script>
function onLoad()
{
    this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
function actionFailure(args)
{
    var span = document.createElement('span');
    this.element.parentNode.insertBefore(span, this.element);
    span.style.color = '#FF0000'
    span.innerHTML = 'Server exception: 404 Not found';
}
function actionbegin(args)
    console.log(args);
    var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0];
    if (args.requestType == 'delete')
    { 
        grid.dataSource.dataSource.headers = []; 
        grid.dataSource.dataSource.headers.push(
        {
            'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val(),
            "KeywordId": args.data[0].KeywordId,
            "UserId": args.data[0].UserId
        }); 
    } 
</script>

1 Reply

PS Pavithra Subramaniyam Syncfusion Team August 12, 2019 06:53 AM UTC

Hi ajit, 
 
Thanks for contacting Syncfusion Support. 
 
You can get the error message from server in the “actionFailure” event by throwing an exception. Please refer to the below code example for more information. 
 
<script> 
    function failure(args) { 
        var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0];  //extract the message from args 
        alert(errorMessage); 
    } 
</script> 
 
[Server side code] 
 
public async Task<JsonResult> OnPostInsert([FromBody]SyncfusionControlCrudModel<Keyword> syncfusionControlCrudModel) 
    { 
      var keywordText = syncfusionControlCrudModel?.value?.Value?.ToLowerInvariant(); 
      if (string.IsNullOrEmpty(keywordText) || keywordText.Length<4) 
      { 
        ModelState.AddModelError(string.Empty, "The keyword text needs to be at least 3 characters"); 
       throw new Exception("The keyword text needs to be at least 3 characters "); //Add custom exception message 
; 
      } 
      .   .   . 
      return new JsonResult(syncfusionControlCrudModel.value); 
    } 
 
 
Please get back to us if you need any further assistance on this. 
 
Regards, 
Pavithra S. 


Loader.
Live Chat Icon For mobile
Up arrow icon