PrimeDigit – A Design Blog by Will Shaver

October 19, 2008

Stack Overflow! MVC Model Binders Gone Awry

Filed under: Uncategorized — Will @ 10:00 am

The recently released MVC Beta 1 has some pretty cool features. One of the features that I’ve been using for a while is Model Binders. These are now turned on by default which can cause some problems if you’re not expecting this behavior. For example, say you have an action as follows:

        public ActionResult Binder(Dog dog)
        {
            return View("Index");
        }

Here’s the model:

    public class Dog
    {
        public string Name { get; set; }
        public Dog cat { get; set; }
    }

Calling this action will cause a stack overflow error, as the default model binder will attempt to recreate every dog in the chain.

I would not have noticed this as I use my own implementation of the model binders to provide functionality for pulling an entity from NHibernate that is passed by ID via Ajax. The problem is that I recently switched over to the [Transaction]/ATM method of managing transactions with NHibernate. The problem with this method is that it creates a dynamic proxy of your controller so it can intercept the transaction. In doing so it looses the method argument attributes and then – boom – stack overflow. So now I’m off to hack YET ANOTHER open source project to see if I can get it to not remove the method parameter attributes. :(

3 Comments »

  1. Ah, that’s a bummer. Just remember: the community appreciates your hard work! ;)

    Comment by Ben Scheirman — October 20, 2008 @ 6:48 am

  2. I gave up on that idea when I found out it was a reported bug marked as “Won’t Fix”. I figure if the current committers figured it was too difficult to fix, I didn’t need to bang my head against it for a day. I implemented it via ActionFilters in MVC and have submitted that to MvcContrib.

    Comment by Will — October 20, 2008 @ 8:29 am

  3. This is the same problem reported at http://forums.asp.net/t/1335683.aspx. The team is deciding how best to go about fixing this issue, but that link provides some immediate workarounds to unblock you.

    Comment by Levi — October 20, 2008 @ 11:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress