Another undocumented “feature” I ran into when playing with the MVC.
I have an ajax action link that sorts a list on the page. To accomplish this, the link performs an ajax request to the controller’s Sort action and in the end redraws the part of the page contianing the list’s data with the new sort settings.
No biggie… except that when using Mozilla Firefox, it never worked right. The list would be redrawn, but would always contain the entire page’s content including the menus, headers, and all the other stuff.
This was another one of those things I had hoped would magically go away with the RTM of the MVC framework… but when it didn’t I had to go figure it out.
The process here was a little more complex than what you’d see in the examples and tutorial apps, but not by much. Here is how it worked.
The controller has two actions: List and Sort
The List action reads the user’s profile and gets data based on the user’s sort and filter preferences. It will render a partial view containing just the list’s data if the request is an ajax request, otherwise it renders the entire page view.
The Sort action simply updates the user’s profile with their new sort preferences. Then it uses RedirectToAction to tell the browser to call the controller’s List action again…. and the list action would do the actual list building using the updated settings.
After some debugging though, I found that some browsers don’t resend the necessary headers on a subsequent ajax request when redirected this way. So the list action would think the request was a non-ajax request and it would thus render the whole page view.
There was a lot of discussion about some late beta changes to the MVC framework’s IsAjaxRequest() method, so I had hoped it was just a beta bug… but apparently not.
In order to get this to work reliably, I had to have the sort method add a setting to TempData if it was called from ajax, then have the list method check both the TempData setting AND the IsAjaxRequest() method.
Annoying, but at least there is a workaround.