简体   繁体   中英

non-static ErrorMessage data annotation in MVC

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:

You need to write your own attribute to achieve this. Here is an example:

public class MyReqAttribute : RequiredAttribute
{
    private string _errorID;
    public MyReqAttribute(string errorID)
    {
        _errorID=errorID;           
    }
    public override string FormatErrorMessage(string name)
    {
        string language = !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["l"])? HttpContext.Current.Request.QueryString["l"]: "en-en";
        var translation = RegistrationScriptHelper.Translation.GetRegistrationScript(HttpContext.Current.Request).Find(x => x.Language == language);

        this.ErrorMessage = translation.Item.Find(x => x.Id == errorID 
            && x.Type == "Required").Text;

        return base.FormatErrorMessage(name);
    }

}

And in Global.asax.cs file add following line:

protected void Application_Start()
{
    // other codes here

    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyReqAttribute), 
        typeof(RequiredAttributeAdapter));
}

Then you could use your own attribute in your models:

[MyReqAttribute("FamilyName")]
public string FamilyName { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM