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
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•3w ago
Message Not Public
Sign In & Join Server To View
I am mapping them
I made extension methods to convert between them
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
yeah, I was thinking about making a Country class, that holds the id and the name
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
but at the end of the day, the domain model will only care about the countryId and not the name
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
oh okay got it!
thank you ❤️
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
I was overthinking it because I wanted to keep the countryId around even on the viewmodel
to make changing countries faster
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
yeah, I think I see why
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
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•3w ago
Message Not Public
Sign In & Join Server To View
thank you
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
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!