Why is my DisplayFor not looping through my IEnumerable?

前端 未结 4 1476
难免孤独
难免孤独 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

    This seems like a bug. Html Helper classes are easy to extend, although after looking at the MVC source, looking for the bug, I gave up and just leveraged the premise that templates work for an individual item, so I wrote an HtmlHelper extension that wraps it for you. I took out the lambda expression for my own simplicity, but you can easily go back to that. This example is just for a list of strings.

        public static class DisplayTextListExtension
    {
        public static MvcHtmlString DisplayForList(this HtmlHelper html, IEnumerable model, string templateName)
        {
            var tempResult = new StringBuilder();
    
            foreach (var item in model)
            {
                tempResult.Append(html.DisplayFor(m => item, templateName));
            }
    
            return MvcHtmlString.Create(tempResult.ToString());
        }
    }
    

    Then the actual usage looks like:

                                    @Html.DisplayForList(Model.Organizations, "infoBtn")
    

提交回复
热议问题