c# - Access view model required error message data annotation from Resource file in code - MVC -


i trying access errormessagestring code corresponding message based on user language have difficulties.

in viewmodel have this:

    [required(errormessageresourcetype = typeof(resource), errormessageresourcename = "userfirstnamereq")]     [display(name = "userfirstname", resourcetype = typeof(resource))]     public string firstname { get; set; } 

from english resource file userfirstnamereq = first name required. (this change based on user language selection, german vorname ist erforderlich.)

now have code, want access translated message errormessagestring not accessible. how can value?

public static mvchtmlstring extendedtextboxfor<tmodel, tproperty>(this htmlhelper<tmodel> htmlhelper,expression<func<tmodel, tproperty>> expression, object htmlattributes)     {         var metadata = modelmetadata.fromlambdaexpression(expression, htmlhelper.viewdata);          //if input required in viewmodel, attach required , validation message         if (metadata.isrequired)         {             string erroratribute = geterrormessage(metadata);              return inputextensions.textboxfor<tmodel, tproperty>(htmlhelper, expression, (string) null, output);         }          return inputextensions.textboxfor<tmodel, tproperty>(htmlhelper, expression, (string) null, htmlattributes);     }      private static string geterrormessage(modelmetadata metadata)     {         string retval = string.empty;          var customtypedescriptor = new associatedmetadatatypetypedescriptionprovider(metadata.containertype).gettypedescriptor(metadata.containertype);         if (customtypedescriptor != null)         {             var descriptor = customtypedescriptor.getproperties().find(metadata.propertyname, true);             var req = (new list<attribute>(descriptor.attributes.oftype<attribute>())).oftype<requiredattribute>().firstordefault();              if (req != null)             {                 retval = req.errormessage;                  //here instead of errormessage need errormessagestring             }          }          return retval;      } 

here screenshot of need can't access:

enter image description here

any appreciated. thank time.

if using reflections not issue, try with

retval =  (string)req.gettype().getproperty("errormessagestring", bindingflags.public | bindingflags.nonpublic | bindingflags.instance).getvalue(req); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -