KendoUI MVC Grid - Addnew or Update record with ajax and server side validation

7 posts, 0 answers
  1. Srinivasan
    Srinivasan avatar
    2 posts
    Member since:
    Oct 2012

    Posted 01 Nov 2012 Link to this post

    I have implemented inline editing with Knedo UI MVC grid with Ajax binding, Server side validation handled in controller and sending the error back using -

    ModelState.AddModelError("Error: ", ex.Message);

    @(Html.Kendo().Grid<AnalyticsServiceWeb.ViewModel.SomeViewModel>() .Name("Grid") .Columns(columns => { columns.Bound(p => p.Name); columns.Bound(p => p.Path); columns.Bound(p => p.Space); columns.Command(command => { command.Edit(); command.Destroy(); }); }) .ToolBar(toolbar => toolbar.Create()) .Editable(editable => editable.Mode(GridEditMode.InLine)) )

    function error_handler(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);
        }
    }

    The server side error message is getting displayed when there is a server side exception, but it still completes the action in UI, i mean it adds the new record to the grid and update as well even though there is server side exception.

    Is there any way to retain the state of UI before the action start?, it supposed to work in that way, not sure if i am missing anything?

    Thanks in advance

  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 06 Nov 2012 Link to this post

    Hello,

    I would suggest you to check this code library project which demonstrates how to prevent the Grid from rebinding and keep it in edit mode when there are server side errors.

    http://www.kendoui.com/code-library/mvc/grid/handling-server-side-validation-errors-during-pop-up-editing.aspx


    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Srinivasan
    Srinivasan avatar
    2 posts
    Member since:
    Oct 2012

    Posted 06 Nov 2012 Link to this post

    The server side validation code you posted works with add new record, update existing record, but not the deletion (Ajax binding)
    I am getting below exception on delete, could you please let me know if i am missing anything?


    1. Uncaught TypeError: Cannot read property 'element' of null
      1. (anonymous function)
      2. Class.extend.triggerkendo.all.min.js:9
      3. h.extend.refreshkendo.all.min.js:9
      4. Class.extend.triggerkendo.all.min.js:9
      5. o.extend._processkendo.all.min.js:9
      6. o.extend._changekendo.all.min.js:9
      7. (anonymous function)kendo.all.min.js:9
      8. o.fireWithjquery.min.js:2
      9. g.(anonymous function).call.c.successkendo.all.min.js:9
      10. o.fireWithjquery.min.js:2
      11. d
  4. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 12 Nov 2012 Link to this post

    Hello again Srinivasan,

    The JavaScript error which you see is because there is no editing element when editing a record

    grid.editable.element


    The above is only available when inserting or editing a record. You need to inform the user in a different way that the destroy operation has caused errors.


    Kind regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Pat
    Pat avatar
    58 posts
    Member since:
    Feb 2005

    Posted 14 Dec 2012 Link to this post

    The problem with the referenced link is that it applies to pop up editting.  It doesn't work for me with inline editting.
  6. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 19 Dec 2012 Link to this post

    Hello Pat,

    What do you mean by 'doesn't work'? I tried to change only the following line and the validation seems to be triggered and displayd again.

    .Editable(editable => editable.Mode(GridEditMode.InLine))


    Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Pat
    Pat avatar
    58 posts
    Member since:
    Feb 2005

    Posted 19 Dec 2012 Link to this post

    It does work for me now.

    I initially was getting a javascript error.  But now it works.
Back to Top