Home > Net >  ASP.NET Core displays the last item of information
ASP.NET Core displays the last item of information

Time:02-17

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

CodePudding user response:

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.

CodePudding user response:

You can refer toEnumerable.Last Method

Try:

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