0

In SL5 I have a DataForm that shows data from a DomainService.metadata generated from an Entity Model.

I'm using DataAnnotation.ValidationAttribues such as Required, StringLength, etc, to adorn the data properties in the DomainService.metadata.

These validation attributes work well, once I compile and run, the validations are present in the DataForm.

But I can't get the EnumDataType attribute to work in the DataForm, it's as if it wasn't there:

  public partial class Student {

     internal sealed class StudentMetadata {

        public enum MyEnum {
           One = 1,
           Two = 2
        }

        private StudentMetadata() {
        }

        [EnumDataType(typeof(MyEnum), ErrorMessage = "Type 1 or 2")]
        public Nullable<int> Other { get; set; }

        [Required]
        public int Age { get; set; }

For example, in the DataForm the field Age can't be left empty, but if I type 4 in the TypeOfRoom field, the error message doesn't show.

I know that I could use a ComboBox or something else, but I'm trying to learn the usage of the EnumDataType validation attribute.

1 Answers1

0

You need to change the type of your property Other to the type of the Enum (in your case MyEnum) you support.

    [EnumDataType(typeof(MyEnum), ErrorMessage = "Type 1 or 2")]
    public MyEnum Other { get; set; }
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Thank you for answering. It didn't work with your suggestion. When I type a value that's outside the enumeration, no error is thrown by the DataForm. – user1462071 Sep 11 '12 at 18:15
  • @user1462071: I think you need to use a ComboBox, for selecting possible values of your enum, instead of a TextBox for typing in values. – Jehof Sep 12 '12 at 05:44