Lee
Lee

Reputation: 3969

Preserving complex view model data between post requests

I have a master view model that contains various other view models, including lists of other view models. I can preserve the state of the master view model by using lots of @Html.HiddenFor() statements in views, however this seems untidy, especially when doing so basically reconstructs each member field with the view model. Is there a 'better' way or is this the most common method?

It would be great if a catch all @Html.HiddenFor(model => model.viewModel) automatically included all members.

Upvotes: 0

Views: 68

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46501

It's a better practice to just save the primary keys of objects in hidden fields and re-fetch the data on a POST request. The same counts for populating lists. I usually create a method which takes care of populating my view model with data and call this on both my GET and POST actions.

Upvotes: 1

Related Questions