2013-02-27 55 views
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();,因爲這是我傳遞

不錯啊......有沒有更好的辦法?

回答

0

這幾乎是最好的辦法,但是大多數人會爭辯說你應該在控制器中做,並通過ViewModel將複數名稱傳遞給視圖