ModelState is ignored on read function

4 posts, 0 answers
  1. Dan
    Dan avatar
    200 posts
    Member since:
    Nov 2017

    Posted 25 May 2020 Link to this post

    I saw that when sending the Model state on the read method of the grid it is ignored? Is this function as design? How to implement this on every grid

    public async Task<ActionResult> GetViewModels([DataSourceRequest]DataSourceRequest request)
    {
        var models = System.Array.Empty<ViewModel>();
        ModelState.AddModelError(string.Empty, "Just to test something");
        var result = await models.ToDataSourceResultAsync(request, ModelState).ConfigureAwait(false);
        return Json(result);
    }
  2. Nikolay
    Admin
    Nikolay  avatar
    327 posts

    Posted 27 May 2020 Link to this post

    Hello Dan,

    I suppose here that there is an error in the ModelState which in general triggers the "error" event of the DataSource. By design, the Grid does not do anything to affect the ModelState. Below I am posting an example of handling the server-side errors in the Kendo UI Grid:

    Another useful article I am posting for you is demonstrating an approach of using Async/Await in the Kendo UI Grid's action methods:

    Let me know if you have any questions.

    Regards,


    Nikolay
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. Dan
    Dan avatar
    200 posts
    Member since:
    Nov 2017

    Posted 27 May 2020 in reply to Nikolay Link to this post

    Hi Nicolay,

    I do not understand your response. Like I said the the post if you add an error to the ModelState on the read method of the grid, the kendo ui grid ignores the error. The kendo ui will not handle the errors on the read. My grid handles the grid error event and the error event was not risen when the read method had an error.

    I already know that the ModelState is taken into consideration on methods like Create, Update, Delete. My problem is that I have a case when I need to display a message to the user when the read method is executed. The grid will only display the empty grid.

    Should I assume that the logic of the kendo ui is that the read method will never have errors and if they do the user must not know about them (just clear the grid and)?

  4. Nikolay
    Admin
    Nikolay  avatar
    327 posts

    Posted 29 May 2020 Link to this post

    Hi Don,

    Apologies for the unclear information.

    The Read method can hold an error just as the Update, Create, and Delete. I examined again nthe provided code and I think the problem here is how the error is added to the ModelState. I would suggest change it as follows:

    public async Task<ActionResult> GetViewModels([DataSourceRequest]DataSourceRequest request)
    {
        var models = System.Array.Empty<ViewModel>();
        ModelState.AddModelError("ServerError", "Just to test something");
        var result = await models.ToDataSourceResultAsync(request, ModelState).ConfigureAwait(false);
        return Json(result);
    }

    This has to be handled on the client. Add the DataSource Error event handler.

    .Events(events => events.Error("error_handler"))
    ..
        function error_handler(e) {
            console.log("Error Event Fired!");
            e.preventDefault();
            if (e.status === "customerror") {
               // if (!_.isUndefined(e.errors))
                alert(e.errors.ServerError.errors[0] , 'Error!');
            }
            else
            {
          if (e.errors) {
                  var message = "There are some errors:\n";
                  // Create a message containing all errors.
                  $.each(e.errors, function (key, value) {
                      if ('errors' in value) {
                          $.each(value.errors, function () {
                              message += this + "\n";
                          });
                      }
                  });
                  // Display the message.
                  alert(message);
                }
            }
    
            // Cancel the changes.
            var grid = $("#grid").data("kendoGrid");
            grid.cancelChanges();
      }
    </script>

    For your convenience, I am attaching a small project demonstrating the above. It is an ASP.NET MVC Core one, however, the above implementation is entirely valid for MVC projects.

    I hope this helps.

    Regards,
    Nikolay
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top