Use nameof() to an element of a List of object

  Kiến thức lập trình

I have a model

public class TblStudent 
{
    [Display(Name = "ID")]
    public string? StuID { get; set; }

    [Display(Name = "Full Name")]
    public string? StuFullName { get; set; }
}

I also have a ViewModel

public class StudentIndexViewModel
{
    public IEnumerable<TblStudent> StuTable { get; set; } = new List<TblStudent>();
{

in my View I’m trying to get the property name from TblStudent using this

@(nameof(Model.StuTable.FirstOrDefault().StuFullName))

however, I’m having Sub-expression cannot be used in an argument to nameof. error.
How to display just the property name of the StuFullName property in this case where it’s a List?

LEAVE A COMMENT