从 MCV2 视图中的模型集合中读取 DataAnnotations

发布于 2024-10-07 22:53:06 字数 463 浏览 0 评论 0原文

在我的 MVC2 AdminArea 中,我想为每个域模型创建一个概述表。 我正在使用如下所示的 DataAnnotations 作为这些域模型对象的属性:

[DisplayName("MyPropertyName")]
public string Name { get; set; }

现在我的问题是:如果我的视图收到域模型的集合,如何访问 DisplayName 属性?我需要它来构建在通常循环之外定义的表头

<% foreach (var item in Model) { %>

。在这个循环中,我可以编写

<%: Html.LabelFor(c => item.Name) %>

,但是有什么方法可以使用项目集合而不是具体实例来访问此信息?

提前致谢!

In my MVC2 AdminArea I'd like to create an overview table for each of my domain models.
I am using DataAnnotations like the following for the properties of those domain model objects:

[DisplayName("MyPropertyName")]
public string Name { get; set; }

Now my question is: How can I access the DisplayName Attribute if my view receives a collection of my domain models? I need this to build the table headers which are defined outside of the usual

<% foreach (var item in Model) { %>

loop. Inside this loop I can write

<%: Html.LabelFor(c => item.Name) %>

but is there any way to access this information using the collection of items instead of a concrete instance?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-10-14 22:53:06

有一个 ModelMetaData 类,它有一个名为 FromLambdaExpression 的静态方法。如果您调用它并传入您的属性以及 ViewData,它将返回 ModelMetaData 的实例。该类有一个 DisplayName 属性,应该可以满足您的需要。您还可以从此对象获取其他元数据信息。

例如,您可以创建一个空的 ViewDataDictionary 对象来获取此信息。它可以为空,因为 ModelMetaData 实际上并不使用实例,它只需要泛型类来定义所使用的类型。

//This would typically be just your view model data.    
ViewDataDictionary<IEnumerable<Person>> data = new ViewDataDictionary<IEnumerable<Person>>();

ModelMetadata result = ModelMetadata.FromLambdaExpression(p => p.First().Name, data);
string displayName = result.DisplayName;

即使您没有实际的 Person 对象,First() 方法调用也不会中断,因为 lambda 只是尝试查找您想要元数据的属性。同样,您可以为单个 Person 对象进行此操作:

//This would typically be just your view model data.    
ViewDataDictionary<Person> data = new ViewDataDictionary<Person>();

ModelMetadata result = ModelMetadata.FromLambdaExpression(p => p.Name, data);

您可以使用辅助方法或扩展方法来显着清理它,但这应该会让您走上正确的道路。

There is a ModelMetaData class that has a static method called FromLambdaExpression. If you call it and pass in your property, along with your ViewData, it will return an instance of ModelMetaData. That class has a DisplayName property that should give you what you need. You can also get other meta data information from this object.

For example, you can create an empty ViewDataDictionary object to get this information. It can be empty because the ModelMetaData doesn't actually use the instance, it just needs the generic class to define the type being used.

//This would typically be just your view model data.    
ViewDataDictionary<IEnumerable<Person>> data = new ViewDataDictionary<IEnumerable<Person>>();

ModelMetadata result = ModelMetadata.FromLambdaExpression(p => p.First().Name, data);
string displayName = result.DisplayName;

The First() method call doesn't break even if you have no actual Person object because the lambda is simply trying to find the property you want the meta data about. Similarly, you could d this for a single Person object:

//This would typically be just your view model data.    
ViewDataDictionary<Person> data = new ViewDataDictionary<Person>();

ModelMetadata result = ModelMetadata.FromLambdaExpression(p => p.Name, data);

You could clean this up significantly with a helper or extension method, but this should put you on the right path.

不乱于心 2024-10-14 22:53:06

好吧,我听从了 sgriffinusa 的建议(再次感谢!)并创建了一个强类型的 HtmlHelper:

public static MvcHtmlString MetaDisplayName<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    return MvcHtmlString.Create(metadata.GetDisplayName());
}

当然,TModel 仍然是域模型的集合,就像我最初的问题中所述,但我们可以在视图中调用助手像这样:

<%: Html.MetaDisplayName(p => p.First().Name) %>

Alright, I followed sgriffinusa's advise (thanks again!) and created a strongly typed HtmlHelper:

public static MvcHtmlString MetaDisplayName<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    return MvcHtmlString.Create(metadata.GetDisplayName());
}

Of course TModel still is a collection of domain models like stated in my inital question but we can call the helper in the view like this:

<%: Html.MetaDisplayName(p => p.First().Name) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文