我正在试图找出属性的有效用法。
MSDN说:
一个值,用于对UI中的字段进行分组。
但我不认为这是一个全面的解释。它使我认为GroupName可以用于围绕某些字段创建组框。但接下来的评论是:
不使用此属性获取GroupName属性值。使用GetDescription方法代替。空值或空字符串是有效的。
似乎与此相矛盾。
那么,这个属性是用来做什么的,我应该使用它(可能与自定义模板或自定义ModelMetadataProvider一起使用)来呈现域周围的群框?
发布于 2011-07-11 15:56:19
在MVC RTM源代码中,没有使用的迹象。
"GetDescription“注释可能是文档中的复制/粘贴错误(每个字符串属性似乎都有一个返回可本地化值的GetXXX对应项),因此在本例中它很可能是"GetGroupName”。
更新:我会正确地使用它:从UI角度将属于一起的字段组合在一起。由于这只是模型上的数据注释,它只声明这些字段属于UI上的一个逻辑组,但具体的表示细节取决于基于元数据显示模型的"UI引擎“。我认为在UI上“呈现”这一点最有意义的方法就是您所说的:将分组字段包装到一个区段或字段集中。
当然,将来可能会有MVC或其他自定义扩展基于此属性在UI上进行某种“自动”分组(无需编写检查元数据和生成部分的自定义代码)。但是我很肯定这样的扩展会做一些与你目前所做的非常相似的事情。
发布于 2011-11-10 01:07:57
最后,我编写了这个类,以使GroupName更容易访问:
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
发布于 2017-04-27 14:18:24
,这个怎么样!必须工作:
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)注: NameSpace应该是:System.Web.Mvc
更新:很酷的一点是,如果您为您的dataAnnotation定义了一个MetaDataType类,那么这也会像预期的那样工作。
https://stackoverflow.com/questions/6649505
复制相似问题