asp.net MVC DisplayTemplates和EditorTemplate命名约定 [英] asp.net MVC DisplayTemplates and EditorTemplate naming convention
问题描述
我有几个关于在MVC 2的DisplayTemplates和EditorTemplates的命名约定的问题。
I've got a couple of questions about the naming convention for the DisplayTemplates and EditorTemplates in MVC 2.
例如,如果我有帐户的子列表中的客户对象,我该怎么办:
If for example I have a customer object with a child list of account how do I:
-
创建帐户列表中显示模板,什么叫文件?
Create a display template for the list of accounts, what is the file called?
当我做了的foreach(VAR℃的Model.Accounts)
我怎么叫庙显示,而在foreach循环?当我做 Html.DisplayFor(X => X)
在foreach内 X
是模型,并在此案例 C
。
When I'm doing a foreach( var c in Model.Accounts )
how do I call a display temple while in the foreach loop? When I do Html.DisplayFor( x => x )
inside the foreach x
is the model and not in this case c
.
先谢谢了。
推荐答案
是啊,这是我最喜欢的功能之一,但它是一个有点对一些事情混淆。
Yeah, this is one of my favorite features, but it's a bit confusing for some things.
所以,创造任何类模板,这个名字是根据类型的名称属性,因此,例如,如果你做一个<%= model.GetType()名称%>在你看来,你可以看到什么我的意思是。举个例子,如果你的帐户列表是一个IList,然后调用。名称的类型将返回 List`1
。这是一个合法的文件名,所以如果你把 List`1.ascx
在DisplayTemplates文件夹,它会找到它并使用它。从我所知道的,它不会遍历类层次结构,虽然如此,例如,如果你把一个IEnumerable`1.ascx文件在那里,也不会找到它。
So, creating a template for any class, the name is based on the type's Name property, so for example if you do a <%= model.GetType().Name %> in your view, you can see what I mean. As an example, if your list of accounts is an IList, then the call to .Name on the type would return List`1
. That's a legal file name, so if you put List`1.ascx
in your DisplayTemplates folder, it'll find it and use it. from what I can tell, it won't traverse the class hierarchy, though, so for example if you put an IEnumerable`1.ascx file in there, it won't find it.
要在一个循环中显示,您需要在项目变量传递到拉姆达,所以在你的例子:
To display in a loop, you need to pass in the item variable into the lambda, so in your example:
<% foreach (var c in Accounts){ %>
<li><%= Html.DisplayFor(x => c) %></li>
<%}%>
希望有所帮助。
保罗
Hope that helps. Paul
这篇关于asp.net MVC DisplayTemplates和EditorTemplate命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!