views:

58

answers:

2

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?

  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?
+6  A: 

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.

Paddy
Well I was trying to make the question generic and spark a discussion, but it seems I won't get away with that :) In my particular case, I have a table with 4 columns, only one of which requires user input - the rest if just fluff for the user's sake (not at all needed by the app to make any business logic decisions). So when I would post my form, the controller would do its job with the data it had and then return to the same view, but since the model binder didn't have that extra data sent back, it would vanish, unless I explicitly made sure it was there.
Veli
+3  A: 

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.

Charlie
no point repeating myself :) thanks for the response +1
Veli