how can i give Id of my selected tag in c# this is my code:
Options = new SelectList(_db.CityUserTable, nameof(CityUserTable.CityID), nameof(CityUserTable.CityName));
Options.First(x => x.Value == user.CityID.ToString()).Selected = true;
how can i give Id of my selected tag in c# this is my code:
Options = new SelectList(_db.CityUserTable, nameof(CityUserTable.CityID), nameof(CityUserTable.CityName));
Options.First(x => x.Value == user.CityID.ToString()).Selected = true;
Copyright © 2021 Jogjafile Inc.
You have it backwards. You're setting the property
Selectedtrue whereCityIdmatches. Instead you need to get theValue(Assuming value contains the CityId) whereSelectedis true.Use
?afterFirstOrDefaultto ensure a selected match was found before accessingValue.