I have run into a strange, I assume, bug when working with ViewModels and exceptions. On a view that uses a standard model with nothing but primitive types I will do something like this in the controller if there is an exception:
ModelState.AddModelError("AdminError", ex);
Then on my view I have something like this:
<%= Html.ValidationMessage("AdminError")%>
This works fine and displays the value of ex.Message. However, on a view (lets call it Photos) that is using a ViewModel I receive the following error when using a similar method as show above: 'The value '' is invalid.' This is cryptic at best so I set out to Google but, unfortunately, there was very little information on it. The information I found was related to setters and null values when using the EF but I am not using EF or LINQ to SQL in this particular implementation. I wasn't getting any exceptions that I could see and I even hooked the Application_Error event but it was never entered into.
I began to look through the ModelState collection specifically the value related to the error that I had added. As expected the Error Count was 1 and the Exception property had a reference to the exception that had been added but the Message property was null. So I changed the overload I was using of AddModelError to:
ModelState.AddModelError("PhotoError", ex,Message);
Which successfully displays the exception to the user. Am I missing something or is this a bug?