Answered by:
Localizing an MVC 3 Razor app with Data Annotations

Question
-
I am using DataAnnotations with the EntityFramework and have a few localization questions I'd like to ask:
- I want to be able to display error messages for the specific culture that is being used.
- I have a Model that is using DataAnnotations and I also have several resource (resx) files with the same error message, but in a different language.
- I know that I need to somehow link from the data annotation to the Id of the message in my resource file, but am unsure of the syntax on how to accomplish that.
Say for instance I have the below DataAnnotation file. I need to link the ErrorMessage text to the specific key in my resource file. What would it need to look like by implementing step 3?
In additon, for fields like zip codes or telephone numbers (where it's a different regular expression, would I have to take out the message from the model and apply it to each view that is using the model?
Or, is there a way to specify which regular expression I can use in the model?
using System;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;namespace YeagerTechModel
{
[MetadataType(typeof(Customer_Validation))]
public partial class Customer
{}
public partial class Customer_Validation
{
public short CustomerID { get; set; }[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Company { get; set; }[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string LastName { get; set; }[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }[StringLength(50)]
[DataType(DataType.Text)]
public string Address2 { get; set; }[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }[StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")]
[DataType(DataType.Text)]
public string State { get; set; }[StringLength(10)]
[DataType(DataType.Text)]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string HomePhone { get; set; }[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string CellPhone { get; set; }[StringLength(100)]
[DataType(DataType.Url)]
[Url]
public string Website { get; set; }[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string IMAddress { get; set; }public System.DateTime CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
}
}
Bill YeagerFriday, November 18, 2011 2:42 AM
Answers
-
Hi again,
I'm not quite sure what you are saying now... What do you mean with N-Tier app? Do you have a server application that your MVC is communicating with and this server should do the validation? Or do you say that you only have splitted up your MVC application in several projects?
As for the latter, it doesn't matter which assembly the Models is placed, it should not be a problem with using ErrorMessageResourceType and ErrorMessageResourceName in a MVC app. But you need the resources to be put in the same assembly as your models, not in the MVC app. I have an MVC app that has the data model outside in a seperate assembly, and it works for me.
--Rune
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.- Marked as answer by Bill_Yeager Tuesday, November 22, 2011 6:37 PM
Sunday, November 20, 2011 9:13 AM
All replies
-
Hi Bill!
Well, this si actually more MVC related. Instead of defining ErrorMessage, you can use ErrorMessageResourceType and ErrorMessageResourceName instead. I found an blog article that gives you an explanation here http://haacked.com/archive/2009/12/12/localizing-aspnetmvc-validation.aspx
As for defining a custom RegularExpression I'm not aware of any way to localize this. You should maybe try to ask on http://www.asp.net/forums? A tip is to create a custom validation attribute.
Hope this helps,
--Rune
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- Edited by Rune Gulbrandsen Friday, November 18, 2011 6:40 AM Missed an answer in original post
- Proposed as answer by Holger Kreissl Friday, November 18, 2011 10:27 AM
Friday, November 18, 2011 6:37 AM -
Rune, thanks, I'll try it out and let you know if it works.
I found the only other valid link as well that might seems to help with localization: http://msdn.microsoft.com/en-us/library/gg674880(VS.98).aspx
However, there are only 3 method scripts and no Spanish one. In order for me to try this out, the download supposed to come with an associated method and message script in the localization folder but it doesn't.
I can't find anyplace on the jQuery site to leave this msg that it's missing all these method files.....
btw, I already posted to the asp.net forum and no one has responded.
Bill YeagerSaturday, November 19, 2011 3:37 AM -
I haven't used that one before, so I have no idea..
Sorry,
--Rune
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.Saturday, November 19, 2011 7:37 AM -
Apparently, this solution doesn't seem to work if you are using an N-Tiered architecture (which I am) and have the Model in a separate project.
I tried adding a resource file to the app_GlobalResources, Properties, and another folder after adding a reference to my presentation tier project and with the intellisense, it did not find the Resource file.
I just don't know why this is such a pain to do when trying to use Model classes with MVC.
Any other suggestions would be welcomed.
Bill YeagerSaturday, November 19, 2011 5:35 PM -
Hi again,
I'm not quite sure what you are saying now... What do you mean with N-Tier app? Do you have a server application that your MVC is communicating with and this server should do the validation? Or do you say that you only have splitted up your MVC application in several projects?
As for the latter, it doesn't matter which assembly the Models is placed, it should not be a problem with using ErrorMessageResourceType and ErrorMessageResourceName in a MVC app. But you need the resources to be put in the same assembly as your models, not in the MVC app. I have an MVC app that has the data model outside in a seperate assembly, and it works for me.
--Rune
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.- Marked as answer by Bill_Yeager Tuesday, November 22, 2011 6:37 PM
Sunday, November 20, 2011 9:13 AM -
Rune, that worked great for me (as you previoulsy mentioned: you need the resources to be put in the same assembly as your models, not in the MVC app).
Thanks.
Bill YeagerTuesday, November 22, 2011 6:36 PM