C
C#3w ago
Zee

✅ 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
Zee
ZeeOP3w ago
public async Task<FeedBackFormViewModel> DeleteFromForm(int formId)
{
await _repository.DeleteAsync(formId);
return null;
}
public async Task<FeedBackFormViewModel> DeleteFromForm(int formId)
{
await _repository.DeleteAsync(formId);
return null;
}
public async Task<IActionResult> Index()
{
var forms = await _feedbackFormService.ReadForms();
return View(forms);
}
public async Task<IActionResult> Index()
{
var forms = await _feedbackFormService.ReadForms();
return View(forms);
}
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
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
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
I could just scrap the views model and use the model itself in my views and controller ?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
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
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
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
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
what do I gain from mapping to a view model why do that? is it so the cshtml cant access the databaase?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
ah ok that helps me understand thx
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
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?

Did you find this page helpful?