MVC 6 Tag Helpers and foreach

2020-02-26 03:45发布

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>
}

2条回答
来,给爷笑一个
2楼-- · 2020-02-26 04:05

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

@foreach (var item in Model)
{
    <label asp-for="@item.BookingCode"></label>
}
查看更多
做个烂人
3楼-- · 2020-02-26 04:05

I Have a simple way to do a list and show properties of it.

List<string> razones = new List<string>();
foreach (var item in _context.Reason)
{
    razones.Add (item.Description);
}
System.Diagnostics.Debug.WriteLine(razones.Count);
查看更多
登录 后发表回答