views:

203

answers:

1

I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same approach for a Forms project but I'm not sure how to go about it.

I have set my attributes but they do not seem to get checked when I click Save.

UPDATE: I have used Steve Sanderson's approach which will check for attributes on my class and return a collection of errors like so:

        try
        {
            Business b = new Business();
            b.Name = "feds";
            b.Description = "DFdsS";
            b.CategoryID = 1;
            b.CountryID = 2;
            b.EMail = "SSDF";
            var errors = DataAnnotationsValidationRunner.GetErrors(b);
            if (errors.Any())
                throw new RulesException(errors);

            b.Save();
        }
        catch(Exception ex)
        {

        }

What do you think of this approach?

A: 

Steve's example is a bit dated (though still good). The DataAnnotationsValidationRunner that he has can be replaced by the System.ComponentModel.DataAnnotations.Validator class now, it has static methods for validating properties and objects which have been decorated with DataAnnotations attributes.

Paul