ModelState values override Model values in HtmlHelpers

We ran into an interesting problem in Chrome using MVC, dates, and EditorFor. When we tried to submit a search, the dates kept getting the wrong format applied to them once the page loaded again, and because of this Chrome wouldn't display a datepicker for them anymore, even though it did when the page first loaded. We eventually discovered that this was due to the way MVC operates with query string and model values, and we were able to find a couple solutions for it. Come along we me and let's do a bug hunt!

The Problem

We needed to search a set of data using a date range, and were trying to set it up using the POST-REDIRECT-GET pattern, which specifies that a POST action should only ever return a REDIRECT. But what were noticing was that, in Chrome, the text box inputs for the dates were getting the wrong format!

All the markup looks good, the inputs are in the correct format, everything looks great. When we submitted a search, though, the resulting page looked like this:

What the hell?! The dates are in the wrong format! But we specified DisplayFormat on each property, so why are they showing up as the wrong formats?

It turns our that the EditorFor call was looking at ModelState to get the values of StartDate and EndDate, and having those names appear in the query string causes them to be added to the ModelState. The HtmlHelpers will check ModelState before they check model values, so if a value exists in the ModelState with the name that the helpers are looking for, that value gets displayed.

(As an aside, while this definitely seems like a bug to me, I really don't know how to fix it. It was noticed as far back as MVC 1 and the behavior still exists. Granted, it's not that big a deal to implement the solutions below, but it took us a little while to understand why MVC was behaving this way.)

Two Solutions

So how do we fix this? One way (the nuclear option, really) is supposedly to reset the ModelState during the GET action: