问题
I'm reading several docs on validation in ASP.NET MVC
. Ignoring those that suggest to reinvent the wheel writing your own validation logic, most articles advocate the use of xVal
or Data Annotation Validators, both of which allow declarative validation through decorating models' properties(*).
I think I'll go for xVal
, as it seems to be the most suggested (and thus, I hope, supported). What puzzles me is that I'm using Linq to SQL
, and my models are declared in an automatically generated file (dbml
+ partial.cs
), containing several classes and almost 5000 lines long, so I'm not very eager to edit it, as if I change the db structure I'll have to regenerate it, loosing my changes.
Is there a way to avoid editing that file?
(*) I've also found a Validator Toolkit which also provides declarative validation and doesn't request to decorate models' properties, but the page shows 57 downloads, so I can't tell if the project is mature or not.
回答1:
You can create a partial class and add the Data Annotation Validators to the partial class that way it doesn't matter if your dbml file is regenerated.
Try this asp.net article and this article
回答2:
Check out buddy classes -- really the only way to fly with generated models and DataAnnotation-based validation.
回答3:
Yes it can (xVal). See here and here.
来源:https://stackoverflow.com/questions/1420136/validation-with-asp-net-mvc-linq-to-sql-how-do-i-avoid-editing-a-generated-sour