0

After brief research, I've found in one of SO thread that there are three most popular ways of storing information between requests in ASP.NET MVC:

  1. Storing information in form fields in the view, and post it back to the controller later
  2. Persisting it in some kind of storage, e.g. a file or a database
  3. Storing it in server memory by acessing objects that lives throughout requests, e.g. session variables

How does the first method work? I mean, I can have a controller that returns a view + viewmodel accessible from this view. The user does something in the view and request an action from the server. Now, the action will perform on server side and it will return another view. So anything I store in the HTML form fields will be gone after requesting any action from the server.

I guess the only way is to POST form fields to the server and then send it back with the returned View. If I don't post them from the view, the modelview object data are gone. Or maybe there's another way I'm not aware of?

user107986
  • 1,461
  • 1
  • 17
  • 24
  • 1
    Please make clear what is your Question here.. – ssilas777 Sep 07 '15 at 10:45
  • 1
    The way you've described it is correct. The form fields could be hidden if they're irrelevant to the user. Here's a similar question which has examples of more options: [http://stackoverflow.com/questions/16187983/persisting-values-across-multiple-controller-actions](http://stackoverflow.com/questions/16187983/persisting-values-across-multiple-controller-actions) – markpsmith Sep 07 '15 at 10:49
  • In short, my point is that the information cannot be stored in form fields in the browser, because the view is destroyed after every request to the server. So in order to store the information we need to send the information from the form fields to the server and send them back again in the new view. And my question is if im right here. – user107986 Sep 07 '15 at 10:51

1 Answers1

0

If you need to store some information between requests you can use hidden fields. For example first view contains hidden fields FirstName and LastName. You post some data to your controller and these data are mapped to some view model in action. In this case you should either add you FirstName and LastName properties to this view model or add them as additional action parameters. From this action you want to pass FirstName and LastName to another view:

  1. You can create another view model that contains these properties and initialize them in action
  2. You can use storages like ViewBag, ViewData etc.. to pass FirstName and LastName without adding them to view model.

But if you don't want to send FirstName and LastName through hidden fields you can use TempData storage. Add these parameters in first action that renders some view and you can read them in another action. Read this article for the details.