ASP.NET Core displays the last item of information

Viewed 68

Sorry, I'm a newbie, and I'd like to ask if there's a way to show only the last piece of information.

   @{
    foreach (var item in Model)
    {
        <li>@item.Ax</li>       
    }
}

enter image description here

2 Answers

To display the latest item of the collection:

@Html.DisplayFor(m => Model.LastOrDefault().Ax) 

The LastOrDefault() allows to check if the sequence contains no elements and returns the default value in this case.

You can refer toEnumerable.Last Method

Try:

@{var last = Model.Last();}
@Html.DisplayFor(modelItem => last.Ax)
Related