C
C#3w ago
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
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?
18 Replies
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
I am mapping them I made extension methods to convert between them
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
yeah, I was thinking about making a Country class, that holds the id and the name
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
but at the end of the day, the domain model will only care about the countryId and not the name
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
oh okay got it! thank you ❤️
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
I was overthinking it because I wanted to keep the countryId around even on the viewmodel to make changing countries faster
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
yeah, I think I see why
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
cause I'd click edit and go to /person/{id} yeah so it's inevitable that I'll use it that was so obvious now that I think about it 🤣 omg
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
thank you
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Thanacio
ThanacioOP3w ago
yeah, basically I'll have an init only id so that, when I read from the database I'll be getting that object, and modify it by copying over the values from the viewmodel that's roughly the idea although I heard some people prefer to insert the whole resource instead of doing PUT on edit I'll be honest, I underestimated the complexity of something that is so simple in first sight 😅 thank you again for your help! have a good day/night!

Did you find this page helpful?