Custom Attribute in c# to give Custom data-attribute in html -


i'd able make data attribute in c# object in model. when displayed. i'd html render value in data attribute within html itself.

in head, code looks similar this.

public class affectsattribute:attribute {     public string[] departments { get; set; }  }   [emailaddress] [required(errormessage = "email required")] [affects(departments = new string[]{"coaches"})] public string email {get; set;} 

then in html, after being called razor this

@html.editorfor(model => model.email)   <input class="text-box single-line" data-affects="coaches" data-val="true" data-val-email="the email field not valid e-mail address." data-val-required="email required" id="email" name="email" type="email"  value=""> 

i have no clue how specify additional data attribute added when item rendered on page. can help?

it can done through validationattribute. if inherit this:

public class mycustomattribute : validationattribute {     protected override validationresult isvalid(object value, validationcontext validationcontext)     {     }      public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context)     {     } } 

the getclientvalidationrules interested in. return ienumerable of attributes want client have.

you can check out article on codeproject.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -