为什么我的DisplayFor通过我的IEnumerable&LT不循环,日期时间&GT ;? [英] Why is my DisplayFor not looping through my IEnumerable<DateTime>?

查看:211
本文介绍了为什么我的DisplayFor通过我的IEnumerable&LT不循环,日期时间&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这条线在我看来

@(Html.DisplayFor(m => m.DaysOfWeek, "_CourseTableDayOfWeek"))

其中, m.DaysOfWeek 的IEnumerable< D​​ateTime的方式>

有是_CourseTableDayOfWeek.cshtml的内容:

There is the content of _CourseTableDayOfWeek.cshtml:

@model DateTime
@{
    ViewBag.Title = "CourseTableDayOfWeek";
}
<th>
    @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int) Model.DayOfWeek]
    <span class="dateString">Model.ToString("G")</span>
</th>

和我得到以下错误:

传递到字典中的模型产品类型 System.Collections.Generic.List`1 [System.DateTime的] ,但这部词典需要一个模型项目类型的的System.DateTime

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.DateTime]', but this dictionary requires a model item of type 'System.DateTime'.

如果我指的是下面的帖子:

If I refer to the following post:

http://stackoverflow.com/a/5652524/277067

DisplayFor 应通过IEnumerable的循环,并显示每个项目的模板,不应该吗?

The DisplayFor should be looping through the IEnumerable and display the template for each item, shouldn't it?

推荐答案

这不是循环,因为您为显示模板的 DisplayFor 助手的第二个参数指定一个名称( _CourseTableDayOfWeek )。

It's not looping because you have specified a name for the display template as second argument of the DisplayFor helper (_CourseTableDayOfWeek).

只有当你依靠公约循环即。

It loops only when you rely on conventions i.e.

@Html.DisplayFor(m => m.DaysOfWeek)

,然后在〜/查看/共享/ DisplayTemplates / DateTime.cshtml

@model DateTime
@{
    ViewBag.Title = "CourseTableDayOfWeek";
}
<th>
    @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int) Model.DayOfWeek]
    <span class="dateString">Model.ToString("G")</span>
</th>

在您指定显示模板自定义名称(作为DisplayFor助手的第二个参数或 [UIHint] 属性)它会为集合属性不再循环该模板将完全通过了的IEnumerable&LT; T&GT; 为模型。

Once you specify a custom name for the display template (either as second argument of the DisplayFor helper or as [UIHint] attribute) it will no longer loop for collection properties and the template will simply be passed the IEnumerable<T> as model.

这是令人困惑,但是这是怎么回事。我不喜欢它的。

It's confusing but that's how it is. I don't like it either.

这篇关于为什么我的DisplayFor通过我的IEnumerable&LT不循环,日期时间&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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