I have a site which can be opened in multiple languages, the strings from the site are retrieved from a XML file which is provided by the product owner.
The model contains many fields but for this question we are just looking at FamilyName
public class RegisterViewModel
{
public Translation Translation { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "LastNameEnter")]
[Display(Name = "Last Name")]
public string FamilyName { get; set; }
}
I previously used to fetch validation and required error messaged for the fields on my models using the above format. Now though we have a helper which reads the XML file and creates a Translation object which contains a list of "Item", each Item is a string with some other properties.
I have tried to change the fields on my model to the following format however it doesnt work because I get following error:
An object reference is required for the non static field.
[Required(ErrorMessage = Translation.Item.Find(x => x.Id == "FamilyName " && x.Type == "Required").Text)]
public string FamilyName { get; set; }
How can I go about setting the error message using my non static Translation
property.
The translation property is set in the constructor from the controller.
EDIT:
The issue lies in my Translation
objects instantiation relying upon query strings in the request.
string Language = !String.IsNullOrEmpty(Request.QueryString["l"])? Request.QueryString["l"]: "en-en";
model.Translation = RegistrationScriptHelper.Translation.GetRegistrationScript(Request).Find(x => x.Language == Language);
EDIT 2: Global.asax.cs:
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CustomRequiredAttribute),
typeof(RequiredAttributeAdapter));
Output: