asp.net mvc - Get MaxLength value in .cshtml template -
in class have..
public class mymodel {     [maxlength(30)]     public string name { get; set; } }   in view..
@html.antiforgerytoken() @html.editorformodel()   and string.cshtml
@model string @{      // how cetn maxvalue here ?? }  @html.textbox(html.idformodel().tostring(), model, new { @class= "text-box single-line", placeholder=viewdata.modelmetadata.watermark })      
here basic code attribute off property.
// use nullable int remember (or use regular int , set default, whatever use-case is) int? maxlength; propertyinfo[] props = typeof(mymodel).getproperties(); foreach (propertyinfo prop in props) {     object[] attrs = prop.getcustomattributes(true);     foreach (object attr in attrs)     {         maxlengthattribute maxlengthattr = attr maxlengthattribute;         if (maxlengthattr != null &&  prop.name.equals("name"))         {              maxlength = maxlengthattr.length         }     } }  // check make sure got value. opti if (maxlength.hasvalue) { // whatever want }      
Comments
Post a Comment