A much better approach is to create a custom HTML helper that will correctly perform pluralization using the .NET 4 PluralizationService (in System.Data.Entity.Design.PluralizationServices namespace is a reference to the System.Data.Entity.Design assembly), which is also used by EF6 to pluralize table names.
The Razor helper is as follows:
namespace CustomHelpers { public static class CustomHelpers { public static MvcHtmlString Pluralize(this HtmlHelper htmlHelper, string source) { var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us")); var plural = serv.Pluralize(source); return MvcHtmlString.Create(plural); } } }
You can easily use this helper in Razor with the following syntax:
@using CustomHelpers <div class="jumbotron"> <h1>Hi @Html.Pluralize("person")</h1> </div>
As you can imagine, he correctly pluralizes the Personality to People , Market to Markets and many other words, since it uses the dictionary of plurality. This is much better than using any custom pluralization code.
Faris zacina
source share