views:

201

answers:

3

hi, i want to update different areas of my MVC page after one action (say a click) occurs.

how, i use ajax, and wrapping the entire area where all these parts need to be updated is counter intuitive as the ajax data being sent back would be close to the entire page.

so, i have a few partial views, - but the problem is, each action only returns one view! how can i return multiple views from this one action?

i know a popular solution is to just hammer the server with multiple async javascript ajax requests for each "part" of the view, but this really is unnecessary and inefficient, reconstructing and destroying the page (say) 5 times for just one action, when it should just occur once, and once it does, the server should manage all the partial views then and there.

anyone any ideas? im desperate.

although a beginner to the mvc framework, im sure an elegant solution must exist, as i ahve faith in the MVC guys, surely we aren't expected to DDoS the server for something like this?

A: 

You can include partial views inside another view, just use the HTML.RenderPartial helper.

AUSteve
having them nested you mean? i can't have the visual organization nested, as they are in different areas of the page, hwoever, can i nest the rendering logic without nesting the visual organization?
Erx_VB.NExT.Coder
that way, i can update two different areas with one action call, and i'd be the happiest person on stackoverflow :)
Erx_VB.NExT.Coder
You're probably best just making multiple AJAX calls. I doubt this will "hammer" the server unless constructing the partial views is very heavy, and in that case a full page refresh is still going to be clunky. Either that or redesign your workflow.
AUSteve
+1  A: 

One possible approach is to have a batching action, which your script will call. This action will internally call all partial views, when wrap their results in a JSON object. On the client side, your script will unwrap the partial views results and will put them at the proper places in the page.

However, there are several limitations of such an approach (and of what you want to do in general):

  • it will render the browser cache useless. Since you don't know which of the partial views might have changed, the batching action response should be non-cacheable. On the other hand, if you were to make several calls to each partial view, each of them can control it's caching which can potentially result in substantial bandwidth savings.
  • you are artificially slowing the client. Your network request will take the amount of time to process all partial views rendering on the server side on one thread (unless you try to spin out multiple threads and make the calls in parallel.). On the other hand, if you make multiple calls for each partial view, the browser can make several of them at the same time, which will parallelize the task for you. And since each part of the page will be updated as it's returned, the overall user experience might actually be better.
  • you are hurting load-balancing. If you are load-balancing multiple servers, multiple requests might end up on separate servers, thus creating better response time.

Frankly, if you are going to update multiple parts of the page, but you want to batch that operation, you might as well stop using Ajax and just reload the full page instead.

Franci Penov
Erx_VB.NExT.Coder
how does this batching action call several partial views? in an action, i can only return one partial view, can you call or update other views from a particular action?
Erx_VB.NExT.Coder
A partial view is just a string containing HTML. Your batching action can directly call all the actions you need, HTML encode their results and add them into an JSON array, then return that array as JsonResult.
Franci Penov
As for my opinion (I wouldn't call it professional, as I don't write websites for living :-)), I think you should just make several Ajax calls and be done with it.
Franci Penov
A: 

I would say that sending back html from a ajax call is bad practice. A better option, in my opinion, is to send back json and use that data to create the html client side. To create the html you can use a javascript templateing engine like jtemplates (if you use jquery).

The json data can be sent from one single action if you so like. But that depends on what kind of data it is. If it's very different parts you should probably split it up or not use ajax at all as you are updating most of the page anyway.

Mattias Jakobsson