Here’s what happens:
- Partial views A and B show the same model
- View A changes the data via Ajax (using
Ajax.BeginForm) - View A re-renders itself (controller gets the updated model from the database)
- Now we have to re-render View B as well, because the data has been changed, right?
- We get the updated model from the database again and re-render View B
How do I prevent re-querying the database? Maybe cache the model-instance in the Session?
What’s the “right” way to do this?
<!--works great when the page is rendered
via postback but what about Ajax?--!>
<div>
@Html.Partial(@ViewA", MyModelInstance)
@Html.Partial(@ViewB", MyModelInstance)
</div>
Can you create a new partial View C that contains both A and B. Whenever the model changes you have to invoke the action that returns the View C.
UPDATE:
The other simple solution I see is when View A updates the model instead of re-rendering the view, get the updated model through AJAX and the update HTML portions through javascript. If you are using jquery you can use the template plugin to update the html quite easily.
By this way you can avoid making one more unnecessary request to update the other View B.