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:
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
Post a Comment