2

So in my mvc project's Project.Repository I have

[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
    public string Name { get; set; }

    public string Age { get; set; }

    internal sealed class FalalaMetadata
    {
        [Required(ErrorMessage="Falala requires name.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Falala requires age.")]
        public string Age { get; set; }
    }
}

I use Falala as a model in my Project.Web.AccountControllers, and use a method to get violations. Validating worked when I had

public class Falala
{
    [Required]
    public string Name { get; set; }

    [Required(ErrorMessage="error")]
    public string Age { get; set; }
}

but not after using the partial class from above. I really need to use a partial class. What am I doing wrong here?

Thanks!

1
  • I pasted your code into a new MVC app and the validation works fine. Can you post your controller and view code? Commented Jan 21, 2010 at 1:44

4 Answers 4

1

I tend to use Metadata classes as followed.

[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
    public string Name { get; set; }

    public string Age { get; set; }
}
public class FalalaMetadata
{
    [Required(ErrorMessage="Falala requires name.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Falala requires age.")]
    public string Age { get; set; }
}

Which works fine for me.

The following should also work (and is a better way to implement metadata classes):

[MetadataTypeAttribute(typeof(Falala.FalalaMetaData))]
public partial class Falala
{
    internal sealed class FalalaMetadata
    {
        [Required(ErrorMessage="Falala requires name.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Falala requires age.")]
        public string Age { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's weird, my projects tend to be filled with partial classes from webservices and I have no problem assigning metadata budy classes. What's the DataAnnotation's version you work with ?
System.ComponentModel.DataAnnotations.dll v3.6. Thanks.
1

I ran into a similar problem and finally got it working by putting both the Model class and the Metadata "buddy" class in the same namespace, even though my references seemed ok. I'm kind of a .net noob though so I'm not exactly comfortable with namespaces, could be something else.

Comments

1

Could Internal on the nested class be the reason...?

I had a similiar problem and it seemed to all boiled down to not making the individual fields in the nested metadata class public - wonder if making the whole class internal causes the same problem?

Comments

0

Not sure if this help, but I had a similar problem and spend days on it. At the end it was just a minor change which did the trick for me.

I changed UnobtrusiveJavaScriptEnabled to false in the config file

Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.