Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

I am trying to implement localization to stringlength attribute for all models with the same way i have localized the DisplayName attribute.
The only change is that i will take the new value from the core class mapped attribute not from a resource file.

I am using EntityFramework with CodeFirst

Can anyone help me with that?

Here is my Code:

so suppose i have the core class [Employee] as following:

C#
public class Employee
    {                
        [Key]
        public int EmployeeId { get; private set; }

        [Required, StringLength(500), Index("IX_Employee_EmployeeName", IsUnique = true)]
        public string EmployeeName { get; private set; }
        
    }


Now i have the Model class [EmployeeModel] as following:

C#
[MetadataType(typeof(MetaDataProvider))]
    public class EmployeeModel
    {
        public int EmployeeId { get; set; }

        public string EmployeeName { get; set; }

    }


Here is my MetaDataProvider Class

C#
public class MetaDataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
                                                        Type containerType,
                                                        Func<object> modelAccessor,
                                                        Type modelType, string propertyName)
        {
            var result = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            if (string.IsNullOrEmpty(result.DisplayName) && containerType != null && propertyName != null)
            {
                var keyForDisplayValue = string.Format("{0}_{1}", containerType.Name, propertyName);
                var translatedValue = HR.Resources.DisplayInputsAR.ResourceManager.GetObject(keyForDisplayValue) as string;

                if (!string.IsNullOrEmpty(translatedValue))
                {
                    result.DisplayName = translatedValue;
                }
            }

            return result;
        }
    }



Now i want [EmployeeModel.EmployeeName] to be with the same length of [Employee.EmployeeName].
Posted
Updated 12-Dec-15 19:37pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900