2

is there any approach in customizing data annotations just like adding [Uppercase],[Lowercase],[ProperCase] annotations in the model to convert the model in the said cases.??? i Have tried using modelbinder and it only to be set just one data annotations what i want to achieved is to have a model like this:

[Display(Name="Agent Code", Prompt =  "Agent Code")]
[CustomAttributes(Case="Uppecase", IsTrim=false)]
[Required(AllowEmptyStrings = false, ErrorMessage = ModelConstants.L_MSG_REQUIRED)]
[StringLength(10)]
public string agent_cd { get; set; }

any idea or suggestions how to achieved my desired output??

1 Answer 1

3

Has a way to do it, create your own annotation, like this:

namespace System.ComponentModel.DataAnnotations
{
    public class MakeMeUpperCase : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            try
            {
                validationContext.ObjectType.GetProperty(validationContext.DisplayName)
                .SetValue(validationContext.ObjectInstance, value.ToString().ToUpper() , null);
            }
            catch (Exception)
            {
            }
            return null;
      }
   }
}

And your property:

[MakeMeUpperCase]
public string TurnUpper { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

I already solved my problem. i can also convert it now into Upper proper and lower case, And also it can be trimmed thanks,,

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.