views:

275

answers:

2

In reading up on ASP.NET MVC I came across some wonderful examples of validation where the business rules were associated with the model and the UI merely displayed the set of errors and flagged the form elements associated with invalid input. I think it makes fantastic sense to keep this logic in a single place rather than have every form perform its own unique validation.

Is it possible to achieve this separation in an elegant manner with an ASP.NET Web Application project (webforms)? I can keep the validation rules in the business logic layer and I can have methods that perform validation and return a set of errors. But I can't figure out a good way to flag problematic controls on the UI side.

In MVC the form elements and model are implicitly linked by property names. Should the UI in ASP.NET reference the unique property names of the model (either as ID/name or as a custom attribute)? Should the UI have access to a manually-generated mapping of control names to property names?

+1  A: 

I'm sure I remember hearing some where that some improvements around Data Annotations would be available for WebForms in .NET 4.0, but after trying to search for it online I'm starting to think I dreamt it.

Although I did find this post of a guy that 'rolled his own':

http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html

Kirschstein
+1  A: 

Hi

A way I like to do it is to create CustomValidators, bound to control on the screen and I call my BL validations in the OnServerValidate event. That way, my validation logic stay in one place.

Hope it will help

mberube.Net
Sorry for the delayed response - both answers offered some valuable insight for how to approach this. I think the best response might just be to move to MVC... :)
Mayo
For sure, moving to MVC is also a really good choice (I currently doing it and it's refreshing).
mberube.Net