Home >Backend Development >C++ >How Can I Localize the Display Name of a Model Property in ASP.NET MVC?

How Can I Localize the Display Name of a Model Property in ASP.NET MVC?

Susan Sarandon
Susan SarandonOriginal
2025-01-03 10:27:40826browse

How Can I Localize the Display Name of a Model Property in ASP.NET MVC?

Localization of DisplayName Attribute

The DisplayName attribute is used to specify the display name for a model property. However, if you need to localize the display name based on the current culture, you'll encounter a limitation with using the traditional DisplayName attribute.

In this situation, you can use the new Display attribute introduced in MVC 3 and .NET 4. The Display attribute provides extended functionality, including localization support.

Solution:

To localize the display name of a model property:

public class MyModel {
  [Required]
  [Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))]
  public string name{ get; set; }
}

The Display attribute takes a Name parameter to specify the resource key and a ResourceType parameter to specify the resource file containing the localized strings.

Note:

  • Ensure that your resource file is set to 'Embedded resource' and uses the 'ResXFileCodeGenerator' custom tool.
  • App_GlobalResources and App_LocalResources should not be used with MVC due to compatibility issues.

The above is the detailed content of How Can I Localize the Display Name of a Model Property in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn