Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does validation in ASP.NET MVC 2 actually work?

I am trying to trace through why my ASP.NET MVC 2 validation isn't working, but I cant find enough about HOW it works to be able to do this.

I have followed the steps in this useful article by David Hayden which seems to be the best documentation currently out there, but nothing actually happens.

I get validation when i submit to the server (as I did since Preview 1 when i added data annotations to my model) but I'm not getting any client side validation.

How can i trace through to test? So far I have verified the following obvious things

  • MicrosoftMvcJQueryValidation.js and jquery.validate.min.js files are being downloaded
  • Html.ClientValidationEnabled = true

I cant see easily what is hooking up to which events to know quite how to debug it.

like image 621
Simon_Weaver Avatar asked Oct 17 '09 03:10

Simon_Weaver


People also ask

How does MVC validation work?

Validation is an important aspect in ASP.NET MVC applications. It is used to check whether the user input is valid. ASP.NET MVC provides a set of validation that is easy-to-use and at the same time, it is also a powerful way to check for errors and, if necessary, display messages to the user.

How is validation implemented in ASP.NET MVC?

In ASP.NET MVC model validations are done using Data Annotation, its inherited System. ComponentModel. DataAnnotations assembly. In ASP.NET MVC 4 provides a different way to build custom validation.

How does ASP validation work?

Validation Message Tag Helper (asp-validation-for)It adds the data-valmsg-for="property name" attribute to the element which it carries for example span. It attaches the validation message on the input field of the specified Model property. The client-side validation can be done with jQuery.

How does ASP.NET Process Validation controls?

By default, ASP.NET validation controls perform validation automatically when the page is posted back to the server, after page initialization (that is, after view-state and postback data have been processed) and before your event-handling code is called.


1 Answers

Here's what I've learnt:

MOST IMPORTANT

  • Your HTML Form must be created with the using directive, not just BeginForm and EndForm.
  • You must set Html.ClientValidationEnabled = true BEFORE you start your 'Form'
  • You must use Html.ValidationMessage for each field
  • You must set Html.ClientValidationEnabled = true on each partial control (ascx)

HOW IT WORKS (very simple overview)

  • When you do Html.BeginForm it creates a 'FormContext' in the ViewContext
  • When ValidationMessage helpers are used, metadata is put into the form context
  • When the form is disposed (by the using statement) it writes out all the validation code

MISC

  • I cannot seem to get validation working when I have a partial control, if that control uses a different model from the view that defines the Form.

  • You do NOT need to use Html.TextBoxFor or Html.ValidationMessageFor, you can use Html.TextBox and Html.ValidationMessage

like image 126
Simon_Weaver Avatar answered Sep 21 '22 11:09

Simon_Weaver