I have a dropdownlist. Its not required, so that backing field is nullable. But, when the form loads, the dropdownlist seems to be setting the selected value to the first id (first sequentially).
I build the select list items like this:
var servers = await Context.Servers.OrderBy(s => s.Name).ToListAsync();
servers.Insert(0, new Models.Server() { Id = Guid.Empty, Name = "SELECT ONE" });
return servers.Select(t => new SelectListItem()
{
Selected = t.Id == Guid.Empty ? true : false,
Text = t.Name,
Value = t.Id.ToString()
});
And I render it like this:
@Html.DropDownListFor(m => m.ProductionEnvironment_ServerId, Model.ProductionEnvironment_Servers)
The first sequential Guid in the ID field is selected if ServerId is null. If I make ServerId non-nullable, the drop down becomres required, which it should not be.
Guid.Empty? Furthermore, you could make a slight changeSelected=t.Id == Guid.Empty. If you do so you will have the same result.