在剃刀IEnumerable的特别? [英] Is IEnumerable special in razor?

查看:131
本文介绍了在剃刀IEnumerable的特别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASP.NET MVC剃刀查看详情页,我可以有像模特:

In an ASP.NET MVC razor View detail page I can have a model like:

@model MyClass的

然后做这样的事情:

@ html.DisplayNameFor(M = GT; m.MyProperty)

但是,在一个列表页面,在那里我有:

But in a list page, where I have:

@model IEnumerable的< MyClass的>

同样@html code ++工程。我不指望它,因为myProperty的不是IEnumerable的的属性。在该剃须刀视图引擎知道要看类型参数的MyClass的属性,而不是它本身的IEnumerable

The same @html code works. I wouldn't expect it to, because MyProperty isn't a property of IEnumerable. Is IEnumerable "special" in that the razor view engine knows to look at the properties of the type parameter MyClass, rather than the properties of IEnumerable itself?

推荐答案

这不是太多,IEnumerable的是特殊的。如果你使用@ html.DisplayFor(M => m.MyProperty)你会得到你的IEnumerable列表不包含定义myProperty的错误。你将不得不使用LINQ查询选择一个MyClass的对象或在你的模型做一个foreach()。

It's not much that IEnumerable is special. If you were to use @html.DisplayFor(m => m.MyProperty) you would get an error that your IEnumerable list doesn't contain a definition for "MyProperty". You would have to use a linq query to select one MyClass object or do a ForEach() over your model.

@ html.DisplayNameFor IS在于它具有用于IEnumerable的模型的过载特别。它知道你真的只是该属性的显示名称后,因为你有一个强类型列表它知道如何寻找的显示名称。这是一个方便的功能!

@html.DisplayNameFor IS special in that it has an overload for IEnumerable models. It knows you're really just after the display name for the property and since you have a strongly typed list it knows how to find the display name. It's a convenient feature!

这篇关于在剃刀IEnumerable的特别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆