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.


No comments:

Post a Comment