I have this simple drop down list in my controller:
List<SelectListItem> options = new()
{
new SelectListItem { Value = "True", Text = "Yes" },
new SelectListItem { Value = "False", Text = "No" }
};
options.Insert(0, new SelectListItem { Value = null, Text = "Select" });
ViewBag.options = options;
I use this drop down list for several different yes or no options on the page but the user may not always need yes or no and sometimes it is best to leave the option null. However, when I select the "Select" option, the code tries to use the string "Select" even though I have set the value of that option to be null. All of these drop down options are for columns that are bits and allow nulls.
How can I add a selection that will enter the value null, rather than the string "Select"?
This might be a different question, but I also have this drop down list on the page that uses the same options:
<div class="form-group row">
<b class="col-md-6">Does this project require a bat habbitat assessment?</b>
<select name="Bat" class="form-control col-md-2" asp-items="@ViewBag.options"></select>
</div>
But this list is not tied to a database column and only triggers an email to be sent. For some reason, this box forces me to select yes or no and will not accept the null value; why is this and how can I fix it?