views:

141

answers:

1

In my ASP.Net MVC app I have a Model layer which uses localised validation annotations on business objects.

The code looks like this:

[XmlRoot("Item")]
public class ItemBo : BusinessObjectBase
{
    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(StringResource))]
    [HelpPrompt("ItemNumber")]
    public long ItemNumber { get; set; }

This works well.

When I want to serialise the object to xml I get the error:

"'ErrorMessageResourceType' property specified was not found" (although it is lost beneath other errors, it is the innerexception I am trying to work on.

The problem therefore is the use of the DataAnnotations attributes. The relevant resource files are in another assembly and are marked as 'public' and as I said everything works well until I get to serialisation.

I have references to the relevant DataAnnotations class etc in my nunit tests and target class.

By the way, the HelpPrompt is another data annotation I have defined elsewhere and is not causing the problem.

Furthermore if I change the Required attribute to the standard format as follows, the serialisation works ok.

        [Required(ErrorMessage="Error")]

Can anyone help me?

A: 

Aha, the answer was easier than I expected. In short the "RequiredField" public static property did not exist in the StringResource assembly.

The issue was finding the error. When serialising the object I had to catch the exception on the attempt to instantiate the serialiser

serial = new XmlSerializer(doc.GetType());

and then work my way dfown through a hierarchy of InnerExceptions to analyse the InvalidOperationException that resulted and get the precise error message which told me what was wrong:

The resource type 'StringResource' does not have a publicly visible static property named 'RequiredField'.

Works ok now

Redeemed1