Where to put data management rules for complex data validation in ASP.NET MVC?

Posted by TheRHCP on Stack Overflow See other posts from Stack Overflow or by TheRHCP
Published on 2010-05-21T12:01:28Z Indexed on 2010/05/21 12:20 UTC
Read the original article Hit count: 353

Hello,

I am currently working on an ASP.NET MVC2 project. This is the first time I am working on a real MVC web application. The ASP.NET MVC website really helped me to get started really fast, but I still have some obscure knowledge concerning datamodel validation.

My problem is that I do not really know where to manage my filled datamodel when it comes to complex validation rules. For example, validating a string field with a Regex is quite easy and I know that I just have to decorate my field with a specific attribute, so data management rules are implemented in the model. But if I have multiple fields that I need to validate which each other, for example multiple datetime that need to be correctly set following a specific time rule, where do I need to validate them? I know that I could create my own validation attributes, but sometimes validation ask a specific validation path which is to complex to be validated using attributes.

This first question also leads me to a related question which is, is it right to validate a model in the controller? Because for the moment that is the only way I found for complex validation. But I find this a bit dirty and I feel it does not really fit a the controller role and much harder to test (multiple code path).

Thanks.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc