Marius Vasile

Marius Vasile

  • 602
  • 1.9k
  • 142.5k

asp.net core select list to show value if text is empty

Jul 26 2021 4:51 PM

I have a select that takes UserName as value and FirstName + LastName as Text. User is not required to enter first and last namem so I want the select to show UserName (which is the email use for registration) if FirstName and LastName are empty. I tried the below variant but is not working properly, it either gives only UserName for all or empty spaces

MemberList = await _context.UsersData.Where(s => s.OrgID == orgid).Select(s => new
       SelectListItem
       {
         Value = s.UserName,
         Text = s.FirstName + " " + s.LastName == null ? s.UserName : s.FirstName + " " + s.LastName
       }).ToListAsync();

 


Answers (8)