Reputation: 412
Starting down at the DB schema I have a CHECK constraint and I'm looking for advice on validating this at the model and view levels as well.
I currently have a validation attribute on the model that mirrors the CHECK constraint, and then the view renders a SELECT list from which the user picks.
Database: ADD CONSTRAINT [myConstraint] CHECK (myField IN ('One', 'Two', 'Three'))
Model: [ValueInList(new[] { "One", "Two", "Three" })] public string myField { get; set; }
View: dynamically render a SELECT from a list of allowed values.
This works, but for those of you keeping track that's three copies of the same list, which is just wrong. How should I be implementing this type of scenario?
Upvotes: 1
Views: 1705
Reputation: 27441
Check out MVC Model Validation and MVC Custom Validation using your own custom defined attributes. Basically, you should be able to define your validation logic once on the model/entity level and reuse it in the business tier, web tier, and client-side.
You may also find Remote Validation useful/helpful.
Upvotes: 2