0
我的看法发生在实体直接作为模型:获取视图中的实体的名称 - 最好是复数名称
@model MyProject.Models.MyEntity
有没有一种简单的方法来获取视图中的实体的复数名称?在上面的例子中,我想要MyEntities
。
我知道我可以在助手或控制器中将其传递给ViewBag或ViewModel - 但是在视图中有没有直接获取它的方法呢?
非复数名称会做,因为我已经创建了String
对象pluralises一个字上的扩展方法:
using System.Data.Entity.Design.PluralizationServices;
namespace System
{
public static class StringExtensionMethods
{
public static string Pluralise(this string word, CultureInfo culture)
{
var service = PluralizationService.CreateService(culture);
if (service.IsSingular(word))
{
return service.Pluralize(word);
}
return word;
}
}
PS。如果有人想要使用上述内容,那么需要参考System.Data.Entity.Design
。
编辑:
我才意识到我可以做Model.GetType().Name.Pluralise();
,因为这是我传递
不错啊......有没有更好的办法?