Why is my DisplayFor not looping through my IEnumerable?

前端 未结 4 1477
难免孤独
难免孤独 2020-12-06 05:19

I have this line in my view

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

where m.DaysOfWeek is a

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 05:32

    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)
    

    and then inside ~/Views/Shared/DisplayTemplates/DateTime.cshtml:

    @model DateTime
    @{
        ViewBag.Title = "CourseTableDayOfWeek";
    }
    
        @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int) Model.DayOfWeek]
        Model.ToString("G")
    
    

    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 as model.

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

提交回复
热议问题