4

How can you avoid duplicated validating code in JavaScript and C# in ASP.NET MVC 3, preferably also an example with contextual validation that considers other properties ?

Does ASP.NET MVC 3 make it possible to avoid duplicated validation logic in C# and JavaScript, when you need customized validation ? For an example of the kind of duplication I am talking about, see the blog post http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx , and the comment added by "Ali Nov 20, 2009 12:19 AM", i.e. the C# if statement "if(cents < 0.99 || cents >= 0.995) {" which exists similarly in javascript code.

Is it still (with ASP.NET MVC 3) not possible to define the validation logic only once ? For example, I think it should be possible to write C# code that defines a validation metadata model, and then such a model maybe can be used by C# code and also for automatically generating the javascript code ?

If it is still not supported by the MVC 3 framework itself, maybe are there any external validation libraries that can be used for generating the javascript validation without the need to duplicate it ?

For example, if any of the frameworks http://fluentvalidation.codeplex.com/ or http://specexpress.codeplex.com/ does indeed support automatic generation of client validation code, then can someone please provide a link that illustrate how to use it and put all the pieces together without any manually duplicated (with C# and Javascript) validation code ?

It would also be interesting to see a code example with contextual validation which can validate a property only sometimes (i.e. without using the hardcoded attribute "[Required]" as if the property would always be required in all contexts) or with different validation rules (depending on the value of some other property). For example, let us say you would have a dropdown list with countries and a dropdown list with states. If you choose such a country that indeed have states then the validation should require a state to also be selected, but for other countries that do not have any states then the validation should not be applied. How can such validation be implemented which works at both the client-side and the server-side with as little javascript code as possible that needs to be manually implemented (i.e. I would like to create only a C# validation model that can generate the corresponding javascript validation) ?

1
  • 1
    Given that we are talking about the web, there really isn't any way to completely avoid duplication. Client side validation is nice for avoiding round trips to the server, but it can be bypassed easily so you always have to validate on the server. Data annotations combined with jquery validate certainly reduce what you have to write yourself. Some of the HTML5 features (required and pattern attributes, new input types) could help but support is still lacking. May 29, 2014 at 16:34

1 Answer 1

0

If you use jQuery.validate you can reduce the duplication down to just one function - the actual validation check. Here's an example of how to check if two properties are not equal - essentially the "depending on the value of some other property" example your looking for.

http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.