Home > other >  C# get string value of foreign key
C# get string value of foreign key

Time:02-23

Please consider the follwoing c# code:

    [DisplayName("City")]
    public string City{ get; set; }

    [DisplayName("County/State")]
    public string County { get; set; }

    [DisplayName("Country")]
    public int CountryId { get; set; }
    [ForeignKey("CountryId")]
    public virtual Country Country { get; set; }

How can I get the String value of Country in the same model class? I tried following but CountryId is an int value and Country is null.

public string NameCityCountry => Name " - " City " - " CountryId;

CodePudding user response:

You need to use the Country property to access the Country model, then call the property that you want to display from there, a common implementation would have a Name field:

public class Location
{
    public string Name { get; set; }

    [DisplayName("City")]
    public string City{ get; set; }

    [DisplayName("County/State")]
    public string County { get; set; }

    [DisplayName("Country")]
    public int CountryId { get; set; }
    [ForeignKey("CountryId")]
    public virtual Country Country { get; set; }
}

public class Country
{
    [Key]
    public int Id { get; set; }

    public string Name{ get; set; }
}

Now you could use this syntax:

public string NameCityCountry => Name   " - "   City   " - "   Country.Name;

But I would strongly recommend against setting this as a property, or at the very least include error handling for the cases where Country is null. The problem with a property like this is that now your model is tightly bound to the constraint that the Country property will always be loaded with data.

Formatting is generally best left to the UI level, yes we can cheat by creating a property in the view model that represents the concatenated values from the model but most UI controls will support binding this same expression directly into the presentation surface, it is not an implementation detail that we would try to abstract into our models, it offers no functional value.

CodePudding user response:

you have to use include Country when you select address, otherwise it will be null, for example

Address address = _context.Addresses.Include(i=>i.Country).FirstOrDefault();

you can override ToString() as below , and get this string

string strAddress=address.ToString();

classes

public class Address
{
    public string Name { get; set; }

    [DisplayName("City")]
    public string City { get; set; }

    [DisplayName("County/State")]
    public string County { get; set; }

    [DisplayName("Country")]
    public int CountryId { get; set; }
    [ForeignKey("CountryId")]
    public virtual Country Country { get; set; }

    public override string ToString()
    {
      return    Name   " "   City   " "     County  " "   ( Country==null? "" : Country.Name);
    }
}

public class Country
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}
  • Related