I followed these tutorials:
http://www.asp.net/learn/mvc/tutorial-39-cs.aspx http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/
in order to enforce data validation using Data Annotation for a LINQ-To-SQL-generated class. The metadata class looks like this:
[MetadataType(typeof(PositionValidation))]
public partial class Position
{
}
public class PositionValidation
{
[Required]
public string Title { get; set; }
}
The validation works correctly, but only if I do this in the controller:
if (ModelState.IsValid)
{
_positions.AddPosition(newPosition);
return RedirectToAction("List");
}
If I leave out the check for valid ModelState, it will attempt to add it to the database, even if Title is empty. As a result, I get an entry with a blank title (this applies to edits as well).
I was under the impression that data validation enforces it for the model side too, in addition to the controller/view. Does this mean that I have to add in additional code to do validation in the Position class too? If so, doesn't this violate DRY?