Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:16:35+00:00 2026-06-02T21:16:35+00:00

Say I have a model like this public class User { [Required] [StringLength(14, ErrorMessage

  • 0

Say I have a model like this

public class User
{
    [Required]
    [StringLength(14, ErrorMessage = "Can only be 14 characters long")]
    public string UserName;

}

I want to create a Html helper like this:

@Html.ValidatableEditorFor(m => m.UserName)

so that it spits out a text field with the correct format for jQuery Vaidation plugin to be able to validate, like this:

   <input type="text" class="required" maxlength="14" />

From my research, it seems that there is no way to iterate over all the data annotations in a MetaDataModel so that I can check to see which one’s are applicable to jQuery Validation.

How I envision it working in pseudo code:

    var tag = new TagBuilder("input");
    tag.mergeAttribute("type", "text");
    foreach(var attribute in metadata.attributes)
    {
       CheckForValidatableAttribute(attribute, tag);
    }

...
    private void CheckForValidatableAttribute(DataAnnotation attribute, TagBuilder tag)
    {
        switch(attribute.type)
       {
          case Required:
             tag.addClass("required");
             break;
          case StringLength
             tag.mergeAttribute("maxlength", attribute.value)
             break;
       }
    }

How could I go about achieving a helper like this? I want it to work on data annotations so that I don’t have to duplicate the validation literals.

For instance, the current Html helpers like TextEditorFor do append validatable attributes to their output fields. How does it do this, and how can I make my own implementation?

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:16:37+00:00Added an answer on June 2, 2026 at 9:16 pm

    You may use this simple condition:

    if(attribute.Type is ValidationAttribute)
    {
       string className = attribute.Type.Name.Replace("Attribute", "").ToLower();
    }
    

    UPDATE

    Define an Html helper:

    public static MvcHtmlString ValidationEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, 
            Expression<Func<TModel, TProperty>> expression)
    {
        ....
    }
    

    Create this helper method:

    private static string GetPropertyNameFromExpression<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        MemberExpression memberExpression = expression.Body as MemberExpression;
        if (memberExpression == null)
            throw new InvalidOperationException("Not a memberExpression");
    
        if (!(memberExpression.Member is PropertyInfo))
            throw new InvalidOperationException("Not a property");
    
        return memberExpression.Member.Name;
    }
    

    Now use this in ValidationEditorFor:

    var propertyName = GetPropertyNameFromExpression(htmlHelper, expression);
    var propertyType = typeof(TModel).GetProperties().Where(x=>x.Name == propertyName).First().PropertyType;
    var attributes = propertyType.GetCustomAttributes(true).OfType<ValidationAttribute>();
    

    Now you can check the attributes…. rest is easy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string
Let's say I have a model that looks like this: public class Blog {
Let's say I have a user class setup with validation like this: [MetadataType(typeof(ssUserMetaData))] public
Lets say I have a set of model classes like this: public class Person
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have this object: [JsonObject] public class User { [JsonProperty(PropertyName = id)]
Let's say you have this Model: //model public class Stuff { public string Name
For instance, let's say I have a User model. Users have things like logins,
Let's say I have a model class called Project, but instead of this: class
I'm using FluentNHibernate but NHibernate XML will do. Say I have this model public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.