✅ what to do when I want ref my ID but the class does not have access to the database
so I am learnign MVC and I my code set up is like this
repositiroy - talks to database
service class - talks to repos but maps everything to a view model
controller talks to my service class
I want to delete somethign on my index.cshtml but it uses my ViewModels not my Model class.
so yeah any suggestions? I could delete by name but wonder if there are any other work arounds what do most ppl do in this case
15 Replies
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
fair enough I was thinkinng that too but I saw online that the viewModel shouldnt have the ID because it doesnt do anything with the database. I think the problem i am having is the project is quite small so I am not getting the most out of these code flows like whats the point of having a view model if its just a copy and past of my view?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
I could just scrap the views model and use the model itself in my views and controller ?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
why cant I just scrap the viewmodel and ref the model directly?
just scrap the viewmodel as a whole and replace it with my model which has the id and a link to the databasae
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
ok so your saying that its only for when adding a databaase through dbContext. but why is this any scenario where a viewmodel is better?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
what do I gain from mapping to a view model why do that? is it so the cshtml cant access the databaase?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
ah ok that helps me understand thx
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
ok this makes sense so it also hides the db for security reasons so they cant do anything to it just the stuff in the view model?