'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 "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?
Solution 1:[1]
Do not allow posting unless it is selected?
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Stretto |
Solution 2 | DavidCru |