Ticket Q365189
Visible to All Users

Client Validation in MVC3 with Gridview Template Form

created 13 years ago

I am trying to do client-side validation when entering data into a PopupEditForm called from a GridView (there is a custom edit form template). Although I can successfully validate on the server side (against the Model, using DataAnnotations), client side just doesn't seem to be working.
I have looked at Issue B209769 and tried to understand your response, but am having difficulty. On the one hand DevExpress seems to be indicating that normal MVC client-side validation is not compatible with your editors:
"The standard MVC validation mechanism is incompatible with ours. Thus, you cannot use the [Required] Model attribute together with the ASPxClientEdit.ValidateEditorsInContainerById method, because they do not depend on each other.
If you wish to use DevExpress methods to validate MVCx editors, you need to define validation rules at the level of our editors. If you want to use the [Required] Model attribute, call the Model's methods to validate the editors. "
while on the other hand you link to an MVC extensions demo showing Model Validation in MVC3 (under Data Editors) which says:
"The DataAnnotations attributes cause MVC to provide both client and server validation checks with no additional coding. Client-side validation is activated by attaching the necessary JavaScript validation files to a view's master page and calling the Html.EnableClientValidation method within the view. "
I have added the following two lines to my _Layout.cshtml:
    <script src="…/…/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="…/…/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
added an @{Html.EnableClientValidation();} line at the top of the Partial View containing my GridView (my web.config also has ClientValidationEnabled = true), decorated my model with Data Annotations yet I still can't get the validations working on the client side. Server side is fine, my controller handles the invalid model and reshows the Partial View with the appropriate message. It's just client side that doesn't work.
I'm sure I'm making a simple mistake or perhaps I just don't understand what's going on, I'd just like to be able to get client-side validation working. At the least, could you please point me to an example of it in MVC3/Razor with a GridView and EditTemplateForm? I've looked at the existing Razor example you linked to in the above Issue but that uses a straight HTML form, not a GridView with PopupEdit.
Thanks.

Show previous comments (1)
NK NK
Norman Kleinberg 13 years ago

    Kate:
    I did not see the last two examples you linked to, they are interesting, thanks, however, they both involve using a separate "EditFormPartial" coupled with an "Html.BeginForm" (i.e. the edit form is not set up in the EditFormTemplate within the Gridview).
    Am I correct then in concluding from your reply that there is no way to enable client validation in MVC3 with your extensions (either using unobtrusive JS or MVC) without having a separate Edit Form and Html.BeginForm? If that is the case then OK, I'll know what I'm dealing with, I just want to be sure that I'm not missing anything.
    Thanks.

    DevExpress Support Team 13 years ago

      Hello Norman:
      These are new examples we published yesterday. I am glad to hear that they are interesting and helpful to you.
      Yes, all editors that are validated with model or unobtrusive validation should be wrapped with a form since this is required for correct operation of validation helpers.
      An additional requirement is that the Name of an editor should be the same as the name of the corresponding property in the model. By default, GridView sets custom names for its edit form editors, so the model validation is not available for them. If you wish to validate them with model attributes, use edit form templates. The approach illustrated in our examples is the most optimal solution.
      Thanks
      Kate.

      NK NK
      Norman Kleinberg 13 years ago

        OK, I think I've got it. Thanks much for the detailed explanation.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.