简体   繁体   English

MVC显示用于模板选择

[英]MVC DisplayFor Template Selection

Am I right in saying that in MVC if I have a model entity, lets say Customer, and I use 我说的对吗,在MVC中,如果我有模型实体,可以说客户,然后我使用

@Html.DisplayFor(x=>x.Customer)

it will look for a display template that is "Named" Customer to display the entity? 它将寻找一个名为“命名”客户的显示模板来显示实体?

And, does MVC automatically perform iteration if the object you're passing the template is an IEnumerable or collection of some sort? 而且,如果要传递的模板的对象是IEnumerable或某种类型的集合,MVC是否会自动执行迭代? Eg 例如

@Html.DisplayFor(x => x.AllCustomers)

So if I wanted to create an additional view for Customer and force the helper to use it, say for example 因此,如果我想为客户创建一个附加视图并强迫助手使用它,例如

@Html.DisplayFor(x => x.AllCustomers, "MyCustomTemplate")

then that template would have to be a strongly typed view of type IEnumerable correct? 那么该模板将必须是IEnumerable类型的强类型视图正确吗? And I would have to create the iteration code myself? 而且我必须自己创建迭代代码?

Seems like MVC does the iteration bit if it recognizes that the model entity matches the view's model type. 如果MVC识别出模型实体与视图的模型类型匹配,则好像MVC会执行迭代位。

it will look for a display template that is "Named" Customer to display the entity? 它将寻找一个名为“命名”客户的显示模板来显示实体?

Yes. 是。 And it will use default display template for object if it doesn't find any. 如果找不到object ,它将使用默认的显示模板作为object

And, does MVC automatically perform iteration if the object you're passing the template is an IEnumerable or collection of some sort? 而且,如果要传递的模板的对象是IEnumerable或某种类型的集合,MVC是否会自动执行迭代?

The framework uses built in template for IEnumerable<T> where it performs the iterations, so technically yes. 该框架使用IEnumerable<T>内置模板来执行迭代,因此从技术上讲是可以的。

then that template would have to be a strongly typed view of type IEnumerable correct? 那么该模板将必须是IEnumerable类型的强类型视图正确吗?

Correct. 正确。

And I would have to create the iteration code myself? 而且我必须自己创建迭代代码?

You are right. 你是对的。 Once you specify a custom display template name, the framework will use your template instead of default one, so it has to be a type of IEnumerable<T> . 指定自定义显示模板名称后,框架将使用您的模板而不是默认模板,因此它必须是IEnumerable<T>的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM