Maintain Kendo Grid Edit Mode on MVC ModelState error

7 posts, 0 answers
  1. Sarah
    Sarah avatar
    4 posts
    Member since:
    Apr 2013

    Posted 24 Jul 2013 Link to this post

    Using the MVC3 wrappers for Kendo UI, my grids are wired up to Controllers for their various CRUD operations.  

    In one of my models, I have a series of boolean values (that in turn handle values for checkboxes) where at least one has to be checked before being considered "valid".  As I have not found a good way to handle that kind of validation directly in the model, I am manually checking those 3 booleans in the ActionResult and where all are false, I return an error on the ModelState.  

    What I would like to be able to do is when the grid receives the error in its respective event, to keep the row that was being edited in edit mode.  My attempts to put the row in edit mode manually have not worked, as it appears a sync is firing, which triggers DataBound, which tromps all over my row that I just put into edit mode.  

    Is there a way to:
    1.  Instruct a grid to keep edit mode open (with the original values) when an Error event is triggered from a DataSource save?
    or
    2. Define a custom client-side validation step to perform the check against those booleans before the DataSource Save?
  2. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 26 Jul 2013 Link to this post

    Hello Sarah,

    Preventing the Grid from exiting edit mode is not supported out of the box but can be achieved by using the approach from this code-library. 
    As for creating validation you could define a custom validation attribute (or just extend the Kendo validator)as demonstrated in the "Custom validator editing" demo in the offline examples project that is included with the installation. You could also use the Grid save event and use the event argument preventDefault method to prevent the data to be posted when there are invalid values.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Ezequiel
    Ezequiel avatar
    36 posts
    Member since:
    Oct 2011

    Posted 17 Apr 2014 in reply to Daniel Link to this post

    Hi Daniel, I've downloaded this example linked on your answer to Sarah.
    It worked properly on that project, when I add the same code to my project this args.sender.options.table is null. I have args, sender and options, but table is null.

    I guess it's something that changed in latests versions of Kendo. Is there any other way of doing that?
    I have some Business validation rules on server side that may cause some exception that I add to ModelState and I want to keep edit mode open.

    Thanks,
    Ezequiel
  5. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 21 Apr 2014 Link to this post

    Hello Ezequiel,

    The DataSource events no longer provide a reference to the Grid through the args.sender argument. You could however, get the Grid's instance manually. For example: 
    var grid = $("#Grid").data("kendoGrid");
    grid.table ...
    In case the event handler will be shared among multiple Grids, then you could pass the Grid's ID:
    .Events(events => events.Error("error_handler.bind({WidgetID: '#Products'})"))
     
    ....
     
     
    <script type="text/javascript">
        function error_handler(e) {
            var grid = $(this.WidgetID).data("kendoGrid");
            ....
    </script>


    Regards,
    Alexander Popov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Ezequiel
    Ezequiel avatar
    36 posts
    Member since:
    Oct 2011

    Posted 22 Apr 2014 in reply to Alexander Popov Link to this post

    Today we have an unique javascript function to handle errors. We're migrating from Telerik MVC Extensions to Kendo.
    So, for a basic and logical thing, our team will need to copy a function to every grid we have on our system?
    I don't see any logic in not keeping in Edit Mode by default when the ModelState is invalid...
  7. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 24 Apr 2014 Link to this post

    Hello Ezequiel,

    I am afraid that adding this functionality as built-in one is not currently planned. If you believe that this should be the default behavior then I can suggest to open a feature request in our user voice forum.

    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Samer
    Samer avatar
    1 posts
    Member since:
    Nov 2013

    Posted 21 Jul 2014 in reply to Alexander Popov Link to this post

    Man.. you saved my day with this post... THANKS!!!
Back to Top
UI for ASP.NET MVC is VS 2017 Ready