I have a Nullable bool property named ISFullDay, where I need to have three values:- True, False & Null. Currently inside my asp.net mvc view, I wrote the following on my view:-
@Html.EditorFor(a=>a.ISFullDay)
But the way mvc will deal with it is as follow:- “if the user un-check the checkbox it will pass false”. So in this way I cannot have Null values inside my null able bool , if I use checkbox? So should I modify my view to display two radio buttons instead of checkbox?
Thanks
A checkbox is used for binary choice. The reason that booleans can be Nullable in MVC is because most databases allow nulls for their boolean fields, so the only time when the value would be null would be on already existing entries which have yet to go through Razor (or if the boolean was not set on creation for some reason).
If you really want to be able to re-select null, I would go with Shoe's suggestion of either a 3 radio button set or a dropdown list, which will automatically create the empty selection.
EDIT:
Under some circumstances, it might make sense to have a 2 radio button set when you want null to persist on commit and only change when a selection is made. This will allow null on the boolean field on commit, but once you commit a change you can't go back to it being null without directly changing it in the database.