The Html.DisplayFor
method in itself does not render anything to the response just returns the generated HTML as a MvcHtmlString
.
In order to actually write the rendered HTML to the response you need to tell this to Razor with using the @
sign:
@Html.DisplayFor(x => item, new { renderBreak = renderBreak })
So your whole code should look like this:
@{
var renderBreakInList = Model.Items.Any(x => x.IsSomeType);
foreach(var item in Model.Items)
{
var renderBreak = renderBreakInList && item.IsOtherType;
@Html.DisplayFor(x => item, new { renderBreak = renderBreak })
if (renderBreak)
{
renderBreakInList = false;
}
}
}
Or you can use the WebPageBase.Write
method (which gets called under the hood when using the @
sign):
Write(Html.DisplayFor(x => item, new { renderBreak = renderBreak }));