Veli Gebrev
Veli Gebrev

Reputation: 2521

Ways of keeping model values that are not submitted on post in ASP.NET MVC?

When I have a Model or ViewModel with values that are sent from the controller to the View but rendered in such a way that they are not submitted back to the controller when a form is posted, (e.g. a label whose contents is loaded from a data source) what is the preferred way to make those values available to the controller action that handles the POST request?

  1. Should I include hidden fields for everything I want to get back?
  2. Reload the data I need from the data source? (Yikes!)
  3. Use session variables?
  4. Is there another trick that I am unaware of?

Upvotes: 0

Views: 150

Answers (2)

Paddy
Paddy

Reputation: 33857

I'd suggest re-loading it from the data source - that way you can also perform concurrency checks etc. I guess it depends what kind of scenario you are looking at - what sort of a user load are you expecting?


Based on your new comment above, given the small rate of requests, I'd absolutely go with reloading the data, makes things a lot simpler.

Upvotes: 6

Charlie
Charlie

Reputation: 10307

Without knowing completely the context of what you are doing I can't say for sure, but if you have, say, an edit form which would update an entity and maybe do some logic based on some data in the database, I'd reload your the data you need rather than store it client side and trust what has been sent back in the post is consistent with what you expect the data to be in your data store.

Upvotes: 3

Related Questions