Tuesday, March 24, 2009

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

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.



Monday, March 23, 2009

ASP.NET MVC - Ajax partial update fails in IE when updating a table's contents

I chased my tail for a while last week after the final release of the ASP.NET MVC framework went RTM. I'd been having a few "issues" getting my app working right, especially in IE 8.

I'd been hopeful that the RTM releases of either IE 8 or the MVC framework would magically fix these problems for me, but after both went to RTM last week I discovered that I was going to have to tackle the problem myself.

The main problem was that, in IE 8, I was unable to update a table when I was using Ajax  to fetch a partial view containing the table's contents. I had been planning to use this technique to handle paging and sorting of the table's data and for an auto-refresh of the data periodically.

This worked fine on other browsers, but with IE 8 this always threw an unusually crytpic and unformative Javascript error (an "unknown exception") and the update would not complete.

I did finally get to the bottom of the problem...

As it turns out it has nothing to do with MVC or the beta of IE 8. As it turns out, I was getting bitten by a very old limitation that all versions of IE back to IE 4 share in common.

The Ajax mechanism that performs the partial update operates by simply replacing the innerHTML value of the target elment with whatever it fetches back from the server... in my case it would replace the table's contents with new rows it got from the server.

But IE doesn't support using the innerHTML method on tables directly. After some digging I actually came across an explaination for this limitation from the guy who actually wrote the innerHTML method. If you aren't interested in why, just know that he had really good reasons.

Once I knew that it was the innerHTML property of the table that was the problem, it wasn't too much trouble to fix. I just wrapped the table in a div tag, and set the MVC application's ajax call up so it would  targeted the div instead of the table.

Not a new problem, but the fact that I was working with both a new browser and the new MVC framework made it hard to know I was dealing with a more general problem in the first place.


Saturday, March 21, 2009

So long Sci Fi Channel, but I doubt you will be missed...

The Sci Fi Channel has always been one of the most confusing failures in American Television. Now, they have decided that the reason for the suckitude must just be the name of the channel.

So they are changing the name to "Syfy". Somehow they think that this move will make their crappy shows appeal to a broader range of people.

Reading an article at TV Week about I have to say, the reason this network sucks has never been clearer...

Thinking back on the successes of the Sci Fi channel, and there are some, you end up with a list that goes a little like this (in no apparent order):
  • Stargate (and variants)

  • Battlestar Galactica

  • Dune (mini-series)

  • Eureka
They also had a lot of successes with second-run shows like Doctor Who. But there is one thing that all of these have in common.... they are actually Science Fiction shows.

In the article one of the founders, Mr Brooks, says this:

"We spent a lot of time in the ’90s trying to distance the network from science fiction, which is largely why it’s called Sci Fi"

Oh!

I noticed!

While the Sci Fi channel was making cash from a couple of decent first run Sci Fi shows,They were dumping all their time and money into funding the absolute most amazingly bad screenplays I've ever even heard of.

I mean... "Mansquito"!

WTF?

And that bastardization of Earthsea?

My 9th grade creative writing class wrote better screenplays than that... and I went to public school in a backwoods part South Carolina!

So yeah, we noticed the distance between your network and Sci Fi... really... we did.

Mr Brooks also had this to say:

"The name Sci Fi has been associated with geeks and dysfunctional, antisocial boys in their basements with video games and stuff like that, as opposed to the general public and the female audience in particular"

You know... I get the distinct impression that the problem with the Sci Fi channel isn't that the market doesn't like Sci Fi shows, the problem is that the management at the Sci Fi channel themselves don't like Sci Fi shows.

I think one of the commenters from the TVWeek article (posted by "tijir") said it best:

Kind of funny, some of the biggest shows on television would be a perfect fit on Sci Fi. I'm talking about Heroes, Terminator Sara Conner Chronicals, Chuck, Fringe, and Smallville. Showing that "general" audiences like fantasy/sci fi programming and the one channel that could give it to them big time is "re-branding" itself as siffy.

So Sci Fi managment... go ahead and change the name. It isn't as if the Sci Fi channel has actually been helping put decent content on the air anyway. The few successful shows you've had would have been just as well off, if not better off, on other networks anyway.

I do not expect your network to survive the name change, but after over a decade of watching you guys shit all over the genre you were named after I don't think I care if you make it or not anyway.

I'm just annoyed that someone actually got paid real money to mismanage an entire network for so long.