0

Is it possible to get HTML custom attribute (client-side) when server fire the ValidationResult.

I am doing like this

Model Class :

    public class Model_Test
{
    [Age(18,50)]
    [Display(Name = "My Age")]
    public int Age { get; set; }
}

HTML :

@model CustomValidation.Models.Model_Test
@using (@Html.BeginForm("Index","Test"))
{
    @Html.TextBoxFor(m => m.Age, new { @myValidate="Yes" })
    @Html.TextBoxFor(m => m.Age, new { @myValidate="No" })
    <input type="submit" />
}

Custom Attribute Class :

    public class AgeAttribute : ValidationAttribute
    {
        private readonly int _MinAge = 0;
        private readonly int _MaxAge = 0;
        private const string errorMsg = "{0} must at least {1} or not more than {2}";

        public AgeAttribute(int MinAge, int MaxAge)
            : base(() => errorMsg)
        {
            _MinAge = MinAge;
            _MaxAge = MaxAge;
        }

        //Server-Side Validation
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {



        **// Can we get the HTML Attribute from client side and implement some condition at here???
        // etc...
        // if (html.attribute("myValidate") == "Yes") {
        //    *condition......*
        // } else {
        //    *condition......***
        // }



            if (value != null)
            {
                int data = (int)value;
                if (!(data > (int)_MinAge && data < (int)_MaxAge))
                {
                    return new ValidationResult(null);
                }
            }
            return ValidationResult.Success;
        }
    }

In my code , I got 2 textboxes and each of them with custom attribute "myValidate="Yes/No" .
Can I bring this attribute to server-side ValidationResult for my validation purpose? If not, Is there any other proper way to do this?

2
  • I don't understand why you would want to have two textboxes with the same property and one without validation? Seems odd Commented May 30, 2013 at 8:24
  • Actually my purpose is create a dynamic table IEnumerable<CustomValidation.Models.Model_Test> to let user add row and fill in some value on the age properties and each row might have different condition. Commented May 30, 2013 at 10:22

1 Answer 1

2

You are on the right tracks but the best way to have one field with validation and another without is just use two separate properties and only annotate one with the custom attribute:

public class Model_Test
{
    [Age(18,50)]
    [Display(Name = "My Age")]
    public int Age { get; set; }

    [Display(Name = "Another age")]
    public int AnotherAge { get; set; }
}

Then within your controller you can do what you like with the property and avoid the need to make your validation code more complex.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Stokedout, thanks for your replied. But i don't want to separate the properties into two. Actually my purpose is create a dynamic table IEnumerable<CustomValidation.Models.Model_Test> to let user add row and fill in some value on the age properties and each row might have different condition. That's why i'm thinking how to let server-side decide which condition should use.

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.