I had the following peculiar bug with the way Model Errors were presented in the view for my ASP.NET MVC 2 website. The code for the view is below.
You can see the ordering of the fields is First Name, Last Name, Name and Phone. As you can imagine, the validation messages should reflect this ordering. However I was getting the following output instead.
This isn’t a show stopper issue, but it is the kind of things that customers notice (and good QA people). After registering, users add more information at this screen. For this reason, it should be as elegant as possible to keep them coming back. I assumed this would be an easy fix. I’d just go to the validation code and fix the ordering of the errors.
You can see from the code that it is yielding the validation errors the same way that I would like them to be presented. As it turns out, this didn’t make a whole lot of difference. Then I realized that the validation code was not the issue at all.
The errors are displayed in the order that the properties are listed in the mode.
I’ll give you a moment to allow that to sink in. It’s an issue that is difficult to diagnose but easy to rectify. The original model property ordering below outputs the incorrect ordering.
The model should appear like below to achieve the desired user experience.
Once you make that change, the errors display like below.