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:
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:
[MetadataType(typeof(MetaDataProvider))]
public class EmployeeModel
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
}
Here is my MetaDataProvider Class
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].