4

Is it possible to add new datatypes to the existing DataAnnotations (I'm not looking for a validator but a raw data type). For example

Currnetly you have

[DataType(DataType.Html)]
public string Footer {get; set;} 

And into the mix you can add ~Views/Shared/EditorTemplates/Html.cshtml

I'd like to be able to add [DataType(DataType.CSS)] I know in theory I could use a UIHint for adding a specific view, but if possible I'd like to do it at an even earlier stage and specify the datatype rather than relying on UI Hints.

Any pointers would be greatly appreciated. A Quick search of S.O seems a lot of answers around Custom meta-data types, custom validators, and multiple datatyps but I can't seem to find one for adding a new core data-type.

2 Answers 2

5

DataType has a second constructor that takes a string. However, internally, this is actually the same as using the UIHint attribute.

Adding a new core DataType is not possible since the DataType enumeration is part of the .NET framework. The closest thing you can do is to create a new class that inherits from the DataTypeAttribute. Then you can add a new constructor with your own DataType enumeration.

public NewDataTypeAttribute(DataType dataType) : base(dataType) { }

public NewDataTypeAttribute(NewDataType newDataType) : base (newDataType.ToString()) { }
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you can. DataTypeAttribute has a constructor that accepts string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.