MVC 6 Tag Helpers and foreach

Sergey Kandaurov picture Sergey Kandaurov · Aug 9, 2015 · Viewed 12.3k times · Source

What would I give to asp-for property of a label tag helper in order to display items from a collection. The code below generates a compilation error.

@foreach (var item in Model)
{
    <label asp-for="item.BookingCode"></label>
}

Answer

N. Taylor Mullen picture N. Taylor Mullen · Aug 9, 2015

The @ character escapes the default model lambda code. Therefore you can type:

@foreach (var item in Model)
{
    <label asp-for="@item.BookingCode"></label>
}