I have set my enum property as nullable in my model but when I submit my create form I get 'The value 'Select' is not valid for productColor.'
My Model:
public class product
{
public int productId { get; set; }
public string productName { get; set; }
public color? productColor { get; set; }
}
public enum color
{
Red, Green, Blue
}
My Create View:
<div class="form-group">
<label asp-for="productColor" class="control-label"></label>
<select asp-for="productColor" asp-items="@Html.GetEnumSelectList<color>()" class="form-control">
<option>Select</option>
</select>
<span asp-validation-for="productColor" class="text-danger"></span>
</div>
My Controller:
public async Task<IActionResult> Create([Bind("productId,productName,productColor")] product product)
{
if (ModelState.IsValid)
{
_context.Add(product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(product);
}
Result:
The form appears to post null for the productColor value but the model state is invalid at the controller and the view returns with 'The value 'Select' is not valid for productColor.'