Reddnet Scribbles

It makes me want to gouge my eyes out with a cheese grater!

ASP.NET MVC - After RedirectToAction call, the target action fails to render a partial view correctly

Stephen M. Redd
Wednesday, March 25 2009

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.

Simple enough...  

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. 

 

Stephen M. Redd
Wednesday, March 25 2009
Filed under: Code
Tagged as: , ,
10 Comments

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://reddnet.net/trackback.ashx?id=235

» Comments

  1. Kampanye Damai Pemilu Indonesia 2009 avatar

    i feel lucky can read this usefull news. now i find something what i want to know..

    thank you for this great informations..

    Kampanye Damai Pemilu Indonesia 2009 — April 13, 2009 8:44 AM
  2. Michael Wallasch avatar

    Thanks for this helpful post. Sometimes web development can be so frustrating.

    Michael Wallasch — May 11, 2009 11:34 AM
  3. Sulumits Retsambew avatar

    Wow, I never knew that before, thanks for informing.

    Sulumits Retsambew — June 2, 2009 3:30 PM
  4. Licka avatar

    I've found interesting post here.

    thanks.

    Licka — June 10, 2009 3:41 PM
  5. sulumits retsambew avatar

    Finally I found the MVC special feature here, Thanks for sharing.

    sulumits retsambew — June 15, 2009 3:08 AM
  6. Sulumits Retsambews avatar

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

    Sulumits Retsambews — July 5, 2009 9:54 AM
  7. halı yıkama avatar

    that's great write. Thanks

    halı yıkama — July 10, 2009 5:37 AM
  8. nancy avatar

    thanks for your post. it provides me some useful information about MVC. thank you so much.

    nancy — August 7, 2009 9:39 AM
  9. cheesy avatar

    This was well worth the read thanks for taking the time

    cheesy — August 15, 2009 6:35 AM
  10. migliori promozioni del casinò di Internet avatar

    Thanks for nice post.i really appreciate your work to this site

    migliori promozioni del casinò di Internet — October 14, 2009 4:12 AM

» Leave a Comment