2011-07-11 48 views
2

我試圖找出DisplayAttribute.GroupName屬性的有效用法。什麼是DisplayAttribute.GroupName屬性用於?

MSDN說:

是,用於分組字段在UI的值。

但我不會稱之爲全面的解釋。這讓我覺得GroupName可以用來創建圍繞某些字段的組合框。但後來的評論:

請勿使用此屬性來獲取GroupName屬性的值。 改爲使用GetDescription方法。空值或空字符串爲 有效。

似乎與它相矛盾。

那麼這個屬性是什麼,我應該使用它(可能與自定義模板或自定義ModelMetadataProvider),以便在我的領域呈現groupboxes?

回答

1

在MVC RTM源代碼中沒有使用的跡象。

「GetDescription」註釋可能是文檔中的複製/粘貼錯誤(每個字符串屬性似乎都有一個返回可本地化值的GetXXX對象),所以在這種情況下它應該很可能是「GetGroupName」。

更新: 我會準確地使用它:從UI角度看屬於一起的組字段。由於這只是模型上的數據註釋,它僅聲明這些字段屬於UI上的某個邏輯組,但具體的表示細節取決於基於元數據顯示模型的「UI引擎」。 我認爲在UI上「呈現」這種最有意義的方式正是你所說的:將分組字段包裝成段或字段集。

當然也有可能是MVC或做一些關於「自動」的UI分組的其他自定義擴展的未來擴展(無書面檢查元數據併產生部分自定義代碼)在此基礎上屬性屬性。但我很確定這樣的擴展會做一些非常相似的事情,你現在會做。

+0

它目前沒有使用 - 公平 - 但是設計了什麼?我可以開始將它用於groupbox嗎?我只是不想在MVC實際上開始使用這個屬性來完成不同的事情時,用一隻手在便盆中醒來...... –

+0

它使用它很好 - 我也嘗試使用它,但我不確定如何「得到」它。 –

0

我結束了寫這個類,使組名更容易獲得:

public class ExtendedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider 
{ 
    public const string Key_GroupName = "GroupName"; 

    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) 
    { 
     ModelMetadata modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); 
     DisplayAttribute displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault(); 

     if (displayAttribute != null) 
      modelMetadata.AdditionalValues[ExtendedDataAnnotationsModelMetadataProvider.Key_GroupName] = displayAttribute.GroupName; 

     return modelMetadata; 
    } 
} 

而這個擴展方法:

public static string GetGroupName(this ModelMetadata modelMetadata) 
{ 
    if (modelMetadata.AdditionalValues.ContainsKey(ExtendedDataAnnotationsModelMetadataProvider.Key_GroupName)) 
     return (modelMetadata.AdditionalValues[ExtendedDataAnnotationsModelMetadataProvider.Key_GroupName] as string); 

    return null; 
} 

來源:http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html

0

這個怎麼樣! !必須工作:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Linq.Expressions; 
using System.Reflection; 
using System.Web; 
using System.Web.Mvc; 

namespace System.Web.Mvc 
{ 
    public static class DisplayGroup 
    { 
     public static MvcHtmlString DisplayGroupName(this HtmlHelper helper, string groupName) 
     { 
      return MvcHtmlString.Create(groupName); 
     } 

     public static MvcHtmlString DisplayGroupNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) 
     { 
      var type = typeof(TModel); 
      PropertyInfo propertyInfo = null; 

      var member = (MemberExpression)expression.Body; 
      var property = (PropertyInfo)member.Member; 
      var name = property.Name; 

      var metadataTypeInfo = type.GetCustomAttribute<MetadataTypeAttribute>(); 

      if (metadataTypeInfo != null) 
      { 
       var metadataType = metadataTypeInfo.MetadataClassType; 
       propertyInfo = metadataType.GetProperties().Where(x => x.Name == name).FirstOrDefault(); 
       if (propertyInfo == null) 
       { 
        propertyInfo = type.GetProperties().Where(x => x.Name == name).FirstOrDefault(); 
       } 
      } 
      else 
      { 
       propertyInfo = type.GetProperties().Where(x => x.Name == name).FirstOrDefault(); 
      } 

      string output = ""; 

      var dattr = propertyInfo.GetCustomAttribute<DisplayAttribute>(); 
      if (dattr != null) 
      { 
       if (dattr.GroupName == null) 
       { 
        output = propertyInfo.Name; 
       } 
       else 
       { 
        output = dattr.GroupName; 
       } 
      } 
      else 
      { 
       output = propertyInfo.Name; 
      } 

      return MvcHtmlString.Create(output); 
     } 

    } 
} 




public class MyModel 
    { 
     [Display(Name = "Number",GroupName="Invoice")] 
     string InvNo { get; set; } 
    } 

,然後簡單地寫:

@Html.DisplayGroupNameFor(x => x.InvNo) 

注: 命名空間應該是:System.Web.Mvc

更新: 很酷的事情是,如果你爲您的dataAnnotation定義了一個MetaDataType類,那麼這也將按預期工作編輯。