gpt4 book ai didi

asp.net-mvc-3 - 为什么我的 DisplayFor 没有循环通过我的 IEnumerable

转载 作者:行者123 更新时间:2023-12-03 13:30:03 26 4
gpt4 key购买 nike

我认为这条线

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

在哪里 m.DaysOfWeekIEnumerable<DateTime> .

有_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>

我收到以下错误:

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'.



如果我引用以下帖子:

https://stackoverflow.com/a/5652524/277067
DisplayFor应该循环遍历 IEnumerable 并显示每个项目的模板,不是吗?

最佳答案

它没有循环,因为您已将显示模板的名称指定为 DisplayFor 的第二个参数。助手(_CourseTableDayOfWeek)。

它仅在您依赖约定时循环,即

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

然后在 ~/Views/Shared/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<T>作为模型。

这很令人困惑,但就是这样。我也不喜欢。

关于asp.net-mvc-3 - 为什么我的 DisplayFor 没有循环通过我的 IEnumerable<DateTime>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8678802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com