Grid with DataAnnotations/Validation: How to display field name in the JS error message?

3 posts, 1 answers
  1. Mike
    Mike avatar
    9 posts
    Member since:
    Aug 2012

    Posted 01 Sep 2012 Link to this post

    Hello,

    I have an MVC application that uses the Grid control.  I have configured .NET DataAnnotations, which includes error messages, and it is working.  When there is a data entry validation error, Javascript displays my error message(s).  The issue that I have is that each message that is displayed does not tell me the name of the field that has invalid data.  The following is the Javascript function that Kendo uses in its examples.  I can't find details as how to add the field name to the message that is displayed.  I hope that someone can help me.

    Thanks,

    Mike

    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";
                    });
                }
            });

            alert(message);
        }
    }
    </script>



  2. Answer
    Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 05 Sep 2012 Link to this post

    Hello Mike,

    The key of the error holds the name of the field. You could use it to generate a message which includes the property names for the errors e.g.

    $.each(e.errors, function (key, value) {
        if ('errors' in value) {
            message += key + ":\n";
            $.each(value.errors, function () {
                message += this + "\n";
            });
        }
    });
    Regards,
    Daniel
    the Telerik team
    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. Mike
    Mike avatar
    9 posts
    Member since:
    Aug 2012

    Posted 05 Sep 2012 Link to this post

    Thank you, Daniel!
Back to Top