Error Handling in DetailsTemplate Grid

3 posts, 1 answers
  1. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 25 Feb Link to this post

    I have a sub-grid in a details template, that allows records to be inserted and deleted. I can put an error handler on the datasource, which will show an error message if the action fails, however as the grid name is dynamically generated, I can't see a way to call preventdefault(), to stop the failed inserts / deletes being displayed in the grid.

    How can I get the grid name to call the preventDefault method?

    Grid:-

    <script id="subdetailsTemplate" type="text/kendo-tmpl">
     
        @(Html.Kendo().Grid<EVASLogicLibrary.UserDirectorateMap>()
                  .Name("MapGrid_#=ID#")
                  .Columns(columns =>
                  {
                      columns.Bound(o => o.ID).Title("ID");
                      columns.ForeignKey(p => p.DirectorateID, (System.Collections.IEnumerable)ViewData["directorates"], "ID", "DirectorateName").Title("Directorate").Width(200);
                      //columns.Bound(o => o.UserID).Title("User");
                      columns.Command(command => { command.Destroy(); command.Edit(); });
     
                  })
                  .Events(e=>e.DataBound("gridBind"))
                  .ToolBar(tb => tb.Create())
                  .Editable(e=>e.Mode(GridEditMode.InLine))
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .PageSize(10)
                  .Events(e=>e.Error("Suberror"))
                  .Model(m=>{
                      m.Id(p => p.ID);
                      m.Field(p => p.ID).Editable(false);
     
                  })
                   .Read(read => read.Action("RD_Maps", "Users", new { UserId = "#= ID #" }))
                   .Create(u=>u.Action("InsertMap", "Users", new { UId = "#= ID #" }))
                   .Update(u => u.Action("InsertMap", "Users"))
                   .Destroy(u => u.Action("DeleteMap", "Users"))
                  )
     
                  .Pageable(p => p.Refresh(true))
     
                  .ToClientTemplate()
     
        )
     
        </script>

    Error handler:-

    function Suberror(e) {
     
              if (e.errors) {
     
                  var message = "Errors:\n";
     
                  $.each(e.errors, function (key, value) {
     
                      if ('errors' in value) {
     
                          $.each(value.errors, function () {
     
                              message += this + "\n";
                          });
     
                      }
     
                  });
     
                  alert(message);
              }
     
          }

    And the main grid error handler (that I want to replicate the functionality of:-

    function error(e) {
     
               if (e.errors) {
     
                   var message = "Errors:\n";
     
                   $.each(e.errors, function (key, value) {
     
                       if ('errors' in value) {
     
                           $.each(value.errors, function () {
     
                               message += this + "\n";
     
                           });
     
                       }
     
                   });
     
     
     
                   var grid = $("#Grid").data("kendoGrid");
     
                   grid.one("dataBinding", function (e) {
     
                       e.preventDefault();   // cancel grid rebind if error occurs
     
                   });
     
                   alert(message);
     
            
     
               }
           }

    Thanks

  2. Answer
    Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 29 Feb Link to this post

    Hello AP,

    In order to capture the name of the child Grid you could use the second overload of the Error event and assign a function similar to the following:

            @(Html.Kendo().Grid<ViewModel>()
                .Name("grid_#=ID#") // template expression, to be evaluated in the master context      
                .DataSource(dataSource => dataSource
                    .Ajax()
                    /*..*/
                    .Events(events => events.Error(@<text>
                        function(e) {                      
                           Suberror.call(this, e, "grid_#=ID#");
                        }
                    </text>))               
                )
                /*..*/
                .ToClientTemplate()
             )
    <script>
        function Suberror(e, gridName) {
            var grid = $("#" + gridName).data("kendoGrid");
     
            if (e.errors) {    
                /*..*/
            }
        }
    </script>

    Regards,
    Rosen
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 29 Feb in reply to Rosen Link to this post

    Thanks, I'll give this a go.
Back to Top