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; }
wsyeager36
Member
385 Points
458 Posts
Localizing an MVC 3 Razor app with Data Annotations
Nov 17, 2011 02:11 AM|LINK
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 Yeager
MCP.Net, BCIP