views:

1608

answers:

2

When using Data Annotations Valition, everything works fine until I try to use resources.

Here's the error I get: The name 'ErrorMessageResourceType' does not exist in the current context.

And the code I used:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.Web.Mvc;


namespace Project.Models  
{  
    [MetadataType(typeof(LanguageMetaData))]
    public partial class Language
    {
    }

    public class LanguageMetaData
    {
        [Required(ErrorMessageResourceType(typeof(Resources.Errors)), ErrorMessageResourceName = "IdRequired")]
        public object Id { get; set; }

        [Required(ErrorMessageResourceType(typeof(Resources.Errors)), ErrorMessageResourceName = "NameRequired")]
        public object Name { get; set; }

        public object Flag { get; set; }
        public object IsDefault { get; set; }
    }

}

I can't find anything wrong with this. Can someone more experienced help me with what's wrong?

Thank you!

+3  A: 

It has to be "ErrorMessageResourceType = typeof(Resources.Errors)" instead of "ErrorMessageResourceType(typeof(Resources.Errors))"

This was a small big damn stupid mistake :D

Hope this helps anyone who had the same problem!

Fabio Milheiro
A: 

hi! I have same problem.. initially I did it right:

[Required(ErrorMessageResourceType = typeof(SignIn), ErrorMessageResourceName = "requiredPassword")] public string Password { get; set; }

please advice!!

vlad
I don't know what is your SignIn. It must be a resource. If you don't know more, than put your resource in the App_GlocalResources folder and refer to it as Resources.SignIn in the type of.If that's not the reason why you're having problems, then try substituting 'public string Password { get; set; } for 'public Object Password { get; set; }You don't need to state the type of the property in your data annotations...Apparently, I can't see much wrong about what you did.. Just trying to help. You could paste here the error you're facing!
Fabio Milheiro
Fabio,Thank you very much for your reply. Just to clear out:1. I have my resources in App_GlobalResources2. I have localization worked file in my Views: <label for="Password"><%= SignIn.txtPassword %></label>3. I got no errors when using the data annotations code I've placed before:[Required(ErrorMessageResourceType = typeof(SignIn), ErrorMessageResourceName = "requiredPassword")] public string Password { get; set; }Hope you can explain what is wrong :) I've digged internet and my code like crazy no luck.Thank you once again.
vlad
Sorry I didn't respond earlier. Maybe the answer to your problems is somewhere else. Look for other parts of the code. (Hope you have your problem solved by now).BTW, I stopped using Entity Framework and Data Annotations because it is too damn slow!!! If you need a fast application think twice. You welcome
Fabio Milheiro
Fabio,I use it for validation only.. please check my question http://stackoverflow.com/questions/1481376/asp-net-mvc-validationattribute-and-localized-errormessage I've described it more detailed.Thank you.
vlad