My SelectList based on an enum cannot update the database
I have a simple Html.DropDownList that is backed by a SelectList which, in turn, is backed by my enum.
Enum Colors
Red=1,
Green,
Blue
I add an option label to my Html.DropDownList that shows &q开发者_如何学Gouot;Please select..."
The problem is when I post this page and the value is "Please select..." (int value=0) and do an UpdateModel I get an error saying, "The ViewModel was not successfully updated."
Is there any way around this?
Do not allow posting unless it is selected?
Use a nullable enum in the Controller method or ViewModel. When 0 is selected you can use:
Model.Color.HasValue
to check if a value has been set.
精彩评论