IEnumerable的MVC4模板(MVC4 Template for IEnumerable)

编程入门 行业动态 更新时间:2024-10-19 06:27:35
IEnumerable的MVC4模板(MVC4 Template for IEnumerable)

我想为IEnumerable创建一个模板。

我知道如果我为Employee创建一个模板并调用DisplayFor,它将为整个列表创建,但我想从模板中获取一个表。

如果我将cshtml文件命名为Employee,那么DisplayFor方法,当用于单个Employee时会抛出错误,因为它需要Collection,如果我命名为Collection,它会尝试用于所有集合。

那么,有没有办法可以命名一个只为IEnumerable调用的模板?

编辑 - 我的模板

@model IEnumerable<LearningViews.Models.Employee> <table> <thead> <tr> <th>Name</th> <th>Company</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.Name</td> <td>@item.Company</td> </tr> } </tbody> </table>

I want to create a template for IEnumerable.

I know if i make a template for Employee and call DisplayFor, it will create for the whole list, but i want to get a table from template.

If i name the cshtml file as Employee, then DisplayFor method, when used for a single Employee throws an error because it expects a Collection, if i name Collection, it tries to use for all the Collections.

So, is there a way i can name a template that is called only for IEnumerable?.

Edit - My Template

@model IEnumerable<LearningViews.Models.Employee> <table> <thead> <tr> <th>Name</th> <th>Company</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.Name</td> <td>@item.Company</td> </tr> } </tbody> </table>

最满意答案

正如Andrei所说,您可以明确说明所需模板的名称,也可以使用类上的属性来实现相同的名称。

请查看此答案以获取详细信息: https : //stackoverflow.com/a/2188052/109456

As Andrei stated, you can explicitly state the name of the template you want, or you can use attributes on the class to achieve the same.

Check this answer for details: https://stackoverflow.com/a/2188052/109456

更多推荐

本文发布于:2023-07-17 13:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   IEnumerable   Template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!