如何从 ModelMetadata 检索 GroupName 数据注释
System.ComponentModel.DataAnnotations 中的 DisplayAttribute 有一个 GroupName 属性,它允许您在 UI 控件(例如 WPF/WinForms 中的属性网格)中对字段进行逻辑分组。
我正在尝试在 ASP.NET MVC3 应用程序中访问此元数据,本质上是为了创建属性网格。如果我的模型看起来像这样:
public class Customer
{
[ReadOnly]
public int Id { get;set; }
[Display(Name = "Name", Description = "Customer's name", GroupName = "Basic")]
[Required(ErrorMessage = "Please enter the customer's name")]
[StringLength(255)]
public string Name { get;set; }
[Display(Name = "Email", Description = "Customer's primary email address", GroupName = "Basic")]
[Required]
[StringLength(255)]
[DataType(DataType.Email)]
public string EmailAddress { get;set; }
[Display(Name = "Last Order", Description = "The date when the customer last placed an order", GroupName = "Status")]
public DateTime LastOrderPlaced { get;set; }
[Display(Name = "Locked", Description = "Whether the customer account is locked", GroupName = "Status")]
public bool IsLocked { get;set; }
}
我的视图看起来像这样:
@model Customer
<div class="edit-customer">
@foreach (var property in ViewData.ModelMetadata.Properties.Where(p => !p.IsReadOnly).OrderBy(p => p.Order))
{
<div class="editor-row">
@Html.DevExpress().Label(settings =>
{
settings.AssociatedControlName = property.PropertyName;
settings.Text = property.DisplayName;
settings.ToolTip = property.Description;
}).GetHtml()
<span class="editor-field">
@Html.DevExpress().TextBox(settings =>
{
settings.Name = property.PropertyName;
settings.Properties.NullText = property.Watermark;
settings.Width = 200;
settings.Properties.ValidationSettings.RequiredField.IsRequired = property.IsRequired;
settings.ShowModelErrors = true;
}).Bind(ViewData[property.PropertyName]).GetHtml()
</span>
</div>
}
</div>
那么表单会根据元数据很好地布局,标签、工具提示、水印等都从模型的元数据中提取出来; 但是,我希望能够将这些项目分组在一起,例如在每个组的
The DisplayAttribute in System.ComponentModel.DataAnnotations has a GroupName property, which allows you to logically group fields together in a UI control (e.g. a property grid in WPF/WinForms).
I am trying to access this metadata in an ASP.NET MVC3 application, essentially to create a property grid. If my model looks like this:
public class Customer
{
[ReadOnly]
public int Id { get;set; }
[Display(Name = "Name", Description = "Customer's name", GroupName = "Basic")]
[Required(ErrorMessage = "Please enter the customer's name")]
[StringLength(255)]
public string Name { get;set; }
[Display(Name = "Email", Description = "Customer's primary email address", GroupName = "Basic")]
[Required]
[StringLength(255)]
[DataType(DataType.Email)]
public string EmailAddress { get;set; }
[Display(Name = "Last Order", Description = "The date when the customer last placed an order", GroupName = "Status")]
public DateTime LastOrderPlaced { get;set; }
[Display(Name = "Locked", Description = "Whether the customer account is locked", GroupName = "Status")]
public bool IsLocked { get;set; }
}
and my view looks like this:
@model Customer
<div class="edit-customer">
@foreach (var property in ViewData.ModelMetadata.Properties.Where(p => !p.IsReadOnly).OrderBy(p => p.Order))
{
<div class="editor-row">
@Html.DevExpress().Label(settings =>
{
settings.AssociatedControlName = property.PropertyName;
settings.Text = property.DisplayName;
settings.ToolTip = property.Description;
}).GetHtml()
<span class="editor-field">
@Html.DevExpress().TextBox(settings =>
{
settings.Name = property.PropertyName;
settings.Properties.NullText = property.Watermark;
settings.Width = 200;
settings.Properties.ValidationSettings.RequiredField.IsRequired = property.IsRequired;
settings.ShowModelErrors = true;
}).Bind(ViewData[property.PropertyName]).GetHtml()
</span>
</div>
}
</div>
then the form is laid out very nicely based on the metadata, with labels, tooltips, watermarks etc all pulled out of the model's metadata; but, I would like to be able to group the items together, for instance in a <fieldset>
per group. Does anyone know how to get the GroupName out of the metadata, short of writing an extension method for ModelMetadata?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GroupName
不由DataAnnotationsModelMetadataProvider
解析。因此,即使使用扩展方法,也无法直接从 ModelMetadata 对象中获取它。您可以实现自己的提供程序,扩展现有提供程序以添加对 GroupName 的支持,Brad Wilson 在他的博客中进行了解释。
您还可以编写自己的属性,而不是使用
Display(GroupName = )
并实现IMetadataAware
接口,以将组名称添加到ModelMetadata.AdditionalValues
。GroupName
is not parsed by theDataAnnotationsModelMetadataProvider
. So there's no way to get it right off theModelMetadata
object, even with an extension method.You could implement your own provider that extends the existing one to add support for GroupName, which Brad Wilson explains in his blog.
You could also write your own attribute instead of using
Display(GroupName = )
and implement theIMetadataAware
interface to add the groupname toModelMetadata.AdditionalValues
.您还可以使用此扩展方法:
然后
You can also use this Extension Method:
Then
另一种方法是使用
ShortName<
DisplayAttribute
类的 /code>属性。它由
ModelMetadata
类公开为ShortDisplayName
属性。这并不完全是您想要的,但它可以让您避免创建另一个属性类...最重要的是,您可以利用
DisplayAttribute
的本地化能力。An alternative could be to use the
ShortName
property of theDisplayAttribute
class. It is exposed by theModelMetadata
class as theShortDisplayName
property.This isn't exactly what you're looking for, but it will allow you to avoid creating another attribute class...and on top of that, you can take advantage of the localization ability of the
DisplayAttribute
.