ASP.Net MVC null reference exception with TextArea name
I have a TextArea html helper method I'm calling in a foreach loop. Basically, when I initially load the View it works fine, but when i reload the View and load postback data, the same TextArea throws a NullReferenceException and yet the variable I'm using in the TextArea as the name of the TextArea is not null. I've attached a picture below for demonstration:
Sorry if it's difficult to see, the blue arrow below is pointing to the variable used to name the TextAr开发者_运维百科ea. Again, it works on initial load, but it errors out on postback when the page is reloaded. I'm not sure what's going on.
Are you using custom validation in your application? If so, this is a known issue in MVC 1 (fixed for MVC 2). See http://forums.asp.net/p/1377232/2908610.aspx for more context and a workaround.
This link seemed to provide the solution. Turns out I was not setting the model value after adding errors to the model state, so it was searching for the model values to place back into the TextArea, they were null of course, hence the error. Very obscure error in my opinion. This StackOverflow post was also very enlightening.
精彩评论