JennieKari
JennieKari

Reputation: 37

How can I put two data in string in model class?

I have these in my model class:

public string City { get; set; }
public string ZipCode { get; set; }

But I want to get it together as well. I tried doing something like this:

[DisplayFormat(City, Zipcode)]
public string CityZipCode { get; set; }

But that didn't work but maybe you get the idea of what I'm meaning. How can I get these together?

Please help me if you can, thanks!

Upvotes: 0

Views: 187

Answers (1)

Jonathan Wood
Jonathan Wood

Reputation: 67195

You can create a property that generates what you want on the fly using the other properties. The advantage of this is that you don't actually need to store the combined string until you need it.

public string CityZipCode => $"{City}, {ZipCode}";

Upvotes: 3

Related Questions