MVC 3多DisplayFor模板 [英] MVC 3 multiple DisplayFor-Templates

查看:129
本文介绍了MVC 3多DisplayFor模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个篮子项目清单的自定义模板。我需要一些不同的模板,因为我有展示的项目,这取决于它是否在网页或邮件的方式不同。现在我的问题是,当我使用默认名称它完美的作品。

I'm trying to make a custom template for a basket item list. I need a few different templates, as I have different ways of displaying the item, depending on if it's on the webpage or in a mail. Now my problem is, that when I use the default name it works flawlessly.

@Html.DisplayFor(b => b.Items)

但是,当我尝试添加一个模板名称,我得到了我的模板需要有一个列表类型的IEnumerable和不BasketItem的厚望。

But when I try to add a template name, I get an expection that my templates needs to be of a list type IEnumerable and not BasketItem.

@Html.DisplayFor(i => basket.Items, "CustomerItemBaseList")

任何想法,我的错误是,为什么它不可能是AP preciated。谢谢你。

Any ideas where my mistake is, or why it's not possible are appreciated. Thanks.

推荐答案

不幸的是模板佣工的限制。如果为一个集合属性指定模板名称的模板不再是集合中的每个项目自动应用。可能的解决方法:

Unfortunately that's a limitation of templated helpers. If you specify a template name for a collection property the template no longer applies automatically for each item of the collection. Possible workaround:

@for (int i = 0; i < Model.Items.Length; i++)
{
    @Html.DisplayFor(x => x.Items[i], "CustomerItemBaseList")
}

这篇关于MVC 3多DisplayFor模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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