C
C#3mo ago
iookei

Sub-expression cannot be used in an argument to nameof.

Working with a ASP.Net Core webapp using NET7 I have a model
c#
public class TblStudent
{
[Display(Name = "ID")]
public string? StuID { get; set; }

[Display(Name = "Full Name")]
public string? StuFullName { get; set; }
}
c#
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
c#
public class StudentIndexViewModel
{
public IEnumerable<TblStudent> StuTable { get; set; } = new List<TblStudent>();
{
c#
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
c#
@(nameof(Model.StuTable.FirstOrDefault().StuFullName))
c#
@(nameof(Model.StuTable.FirstOrDefault().StuFullName))
however, I'm having Sub-expression cannot be used in an argument to nameof. error Anyone know how to display just the property name?
2 Replies
Angius
Angius3mo ago
TblStudent.StuFullName That is the class and the property whose name you want to get In general, what you pass to nameof has to be known at compile time
iookei
iookei3mo ago
ooo understood, thanks