StackoverflowException with ASP.NET MVC

I got this strange Stackoverflow exeption today in my MVC project. I have made some modifications to my data model and all of a sudden i got a stackoverflowexception when i was submitting data via a form.

I started googling about it and found this thread on the asp.net message board. It turns out that the Default modelbinder was causing this. The modelbinder is basically what makes ASP.NET MVC able to automatically bind form post values to your object. You probably know it in some degree if you reading this.

Anyway, to solve the problem you should declare what fields you want to include in the binding by adding an attribute like this. This way the modelbinder wont have to deal with the properties that you do not handle in some way.

public ActionResult Ask([Bind(Prefix="question",Include="Text,Title")]Question question)

According to the post in the forums this is a bug that will be fixed in the next release of the MVC framework. Now back to work!

Read more about both model binders and the bind attribute at Scott Guthries blog post about the beta.

Leave a Reply