views:

40

answers:

3

Hello,

Is there a way to refresh several partial view from the controller ? (return View())

Thanks,

Update1: Example, the content pârt of my screen is divided in 2 parts, on the left a customer list on the right the details customer, the details of the customers selected in the list of the left. If I create an new customer, when I save, I'd lile refresh the list (left part) and see the details (right part)

A: 

Kris,

the only way i can think to do this simply is by having multiple partialviews embeded within the main 'view' to be refreshed. these would then be refreshed in the same cycle. alternatively, you could have custom html helpers embedded within the main view that ran approprite code when the view was refreshed.

As for multiple views from a single action, i don't think this is both a good idea or in any way possible.

of course, rules are there to be broken :)

jim
See update 1 :)
Kris-I
ok - will take a look ;)
jim
A: 

I don't think there is any automatic way to do this, but you could use some convention and:

  • Create a custom view result that takes in multiple partial view results i.e. a MultiplePartialViewResult
  • In the execute of the custom view result, call each the execute method of each of the supplied views'. Make sure to wrap each in a div or some other container for ease of retrieval at the client script
  • Upon receiving the response on the AJAX call in the client script, grab the value from each container and replace it in the corresponding elements matching the partial views initially rendered

For the last step you could use a convention. A simple one would be (if there is only one instance for each partial view) to put the id of target html element to update in the div/container you used to wrap it in the second step.

eglasius
A: 

Based on what you're saying, I think using javascript and ajax to refresh from the server would be best.

You could use Html.RenderPartialAction to achieve DRY by putting it on the page, and then loading it using ajax and javascript.

If you were using jQuery, then something like this would work:

jQuery("#divToReload1").load('Url/To/PartialAction')...
jQuery("#divToReload2").load('Url/To/PartialAction')...

Just put that all inside one function and you'll reload all your partials at once.

You can send data through using the data parameter and catch the callback function to do as you wish.

CubanX