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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

146629 Aug 12,2019 12:07 AM UTC Aug 12,2019 06:53 AM UTC ASP.NET Core - EJ 2 1
loading
Tags: DataGrid
ajit goel
Asked On August 12, 2019 12:07 AM UTC

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>

Pavithra Subramaniyam [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon