C#C
C#10mo ago
56 replies
Thanacio

ASP.NET MVC - need opinion for viewmodel properties

I'm new to ASP.NET MVC, and I'm trying to make a simple project with N-tier architecture.
I'm conflicted with how to represent a Country on my ViewModel.

To put it simply, I want my domain model to only hold the CountryId, but I want the ViewModel to display the CountryName that corresponds to that CountryId.
Right now I use this

    public required string Country { get; set; }
    public required int CountryId { get; set; } // foreign key for the Country table

I'm hiding the CountryId field on the page, but I want to keep it around so that I have fast country reads while editing an entry.

I was wondering if creating a separate Country class to model countries (with id, name) is a good idea, but I don't plan on adding CRUD functionality on the countries themselves. And I also plan to keep the domain model storing just the countryId.

Is there a common practice for this scenario?
Was this page helpful?