我想为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
更多推荐
发布评论