Skip to main content

ASP.NET MVC and AJAX



I have a view which is composed of top, left and bottom headers and a main contents pane. Suppose that during an AJAX request I need to update the HTML of the top, bottom and main panels (the left header should stay the same).





I was wondering what would be the best way to accomplish this. The first thought was to put the main contents panel into a partial and have a controller action that would return PartialView. This wouldn't work because as the action returns only the HTML of the main pane I cannot update the top and bottom headers.





So if I put the top and bottom headers into their own respective partial views I would need my controller action to return multiple partial views. Is this possible at all or I am doing something completely off the track?





I saw that it is possible to render a partial view to a string so I thought that I could use this technique in the action to return a JSON object with 3 properties representing the HTML of the 3 partials that I need to update. But this feels like a very wrong approach to me if possible at all.





Another idea I had was to return a JSON object only containing the data necessary for the partials and use javascript to construct the HTML. But building an UI in javascript looks like a difficult job (The main contents partial uses MvcContrib's GridView with paging and sorting).





So I would really appreciate suggestions on what would be the cleanest approach to handle such scenario. Also an adaptive solution would be great: for example if the user has javascript disabled it would just reload the whole page without AJAX.








UPDATE:





Andrew Siemer suggested placing each section into its own partial view and perform multiple ajax requests. This seems like a perfectly valid approach but unfortunately it is not applicable in my scenario because of the following detail I missed in my initial problem description: the top header is actually used to display error/information messages of events occurring in the main panel. So for example I need to show the error message in case an exception is thrown when fetching the model for the main panel. So only a single request could be made in order to update those two panels.



Source: Tips4all

Comments

  1. You can easily place each section into its own partial view. This would then allow you to make a jquery request for each view once your data has been submitted/changed. Each call could then be pushed into the appropriate section. This is very doable and sounds like the appropriate path.

    I would not expect your controller to return multiple partial views...as this will break SRP!

    Update: In following with your update...you can still fetch the sections separately. This simply takes a bit more ajax coolness on your part in that each fetch can return an error chunk (by way of a JSON request). If any error comes back then those error messages can be displayed in the header. Or...you can make the header request last for errors once all of the other requests have reported back in which case each partial view can toss it's error messages to a session variable for the error state...and then show those messages in the header.

    A big reason for choosing to go with each section as it's own section rather than one uber request may not yet be apparent in the initial design. With each section being controlled separately you can then do fancy things such as caching each section independently, providing more frequent updates in one area over another, etc.

    ReplyDelete
  2. This is a tricky one. How about this?

    Setup a partial request for each component top, left, main which each return a partial view with the data needed for that area. Then every time there is an event in the main panel kick off a refresh of the partials in each area to update them.

    Alternatively have them all on fixed timers to poll for new data.

    ReplyDelete
  3. How about including your top and right view in hidden divs on your main view and moving them into correct places on load?

    ReplyDelete
  4. I know this question has been answered, but thought I'd just pitch in a suggestion:


    Backbone.js and jQuery templating would solve your problems easily.


    (Just gonna leave this here as a suggestion for people reading this question)

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex