Lala
Lala

Reputation: 237

passing complete model from view back to controller

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?

Upvotes: 1

Views: 683

Answers (3)

Bogdan
Bogdan

Reputation: 1393

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

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

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.

Upvotes: 0

James Gaunt
James Gaunt

Reputation: 14783

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.

Upvotes: 1

Related Questions