1

Is there a way to pass the complete model from the view back to the controller (without using JSON please)? My model is a list e.g. List<ExmapleClass>

I want to be able to pass it back to the controller, sort, then pass it back to the view to be displayed, so that I don't have to go back to the database to get the original data.

I guess having the list as a (class) member variable in the constructor will eliminate the need to pass the model back and forth, but do I have other options?

Lala
  • 237
  • 1
  • 7
  • 12

3 Answers3

1

Conceptually within the servicing of a single request the communication from the controller to the view is one way. The controller decides on the view to be rendered, passes it a model, and execution never passes back to the controller.

You could execute a child action from within a view, which may achieve something similar to what you're after, but it's not clear based on your question.

If you're talking about communication that takes place across interaction with the user then you may be able to achieve something like this using TempData, where the view stores information in TempData to be consumed by the next controller that executes.

If your concern is performance based on having to repeatedly query a data source I would strongly advise you think about how to cache this data in a service or data access layer rather then try to use the view / controller interaction as a way of caching.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • Thanks @James, TempData is just what I needed. I can set it in the controller the first time the page loads, and if the user chooses to sort, the data is right there. – Lala Oct 11 '12 at 02:51
  • No problem. Just a couple of things to point out. By default TempData is built on session state, which by default is in-memory. However if you change these default (e.g. use a database session state provider) you'll be hitting the database anyway. Also if you stick to the default in-memory providers it's not guaranteed the session data will be there the next request (e.g. if the application restarts, or the data is ejected from the cache), so you'd need to code for a fall-back to query the database just in case. – James Gaunt Oct 11 '12 at 10:08
0

It can be done with hidden fields, like I posted here: Saving multiple records on submit click into differnt entities in MVC4. Not getting values from view in Controller, but if you don't need this data on view, caching is better solution.

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
0

Sending all the data back to the server will use a lot of bandwidth. I think it would be easier and faster to use JQuery/javascript and sort the data directly on the client side. If you have to send the data to the server side you can XML but not sure if you gain anything by using XML.

Here are a few client side sorters: http://tablesorter.com/docs/ http://www.sendesignz.com/index.php/jquery/76-how-to-sort-items-using-jquery

Bogdan
  • 1,323
  • 15
  • 15