Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • I don't understand this answer either. If you're going to go this route, wouldn't something like vm.RefreshCountries() be better? Surely the View Model already has access to the database. Commented Jul 3, 2019 at 14:59
  • on your webpage you might have a javascript vm.RefreshCountries(). but would you have a serverside vm.RefreshCountries() when you are only populating the vm to return the result that one method? Commented Jul 3, 2019 at 15:17
  • Yes. The VM is responsible for all of the data that is presented to the View. Commented Jul 3, 2019 at 15:19
  • For what it's worth, the endpoint in the VM that is providing countries can always be written in such a way that it goes back to the database each time data is compelled from it. Commented Jul 3, 2019 at 15:21
  • 1
    The ugliness I really want to avoid the most is in the [POST] method of the form. You'd have if (ModelState.IsValid { many lines of code } else { DoSomeStuffIfInvalid(); WhenYou'reFinallyDoneThen(); viewModel.Countries = _dataAccess.GetCountries(); viewModel.xyzDropdown = _dataAccess.GetXyz(); etc. return View(viewModel); because it is very unclear that those properties need to be repopulated and you could easily forget to do it. You could make a RepopulateModel() method but if your controller has 20 pages you'd end up creating 20 such methods. Commented Jul 4, 2019 at 7:44