I have this model of Project with list of Id's, that could be null.
public class Project
{
[Required] [Key] public Guid id { set; get; }
...Other fields
public List<Guid>? workers { set; get; }
}
I'm using JsonConvert.Serialize to store this list in MySql DB.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>(x =>
{
x.HasKey(y => y.id);
x.Property(y => y.workers).HasConversion(to => to == null ? null : JsonConvert.SerializeObject(to),
from => from == null ? new List<Guid>() : JsonConvert.DeserializeObject<List<Guid>>(from));
});
Also i have an editing form for Project
, where is select tag and option with value=""
for nullish value.(@ViewBag.workers - SelectList)
<div >
@if (ViewBag.workers != null)
{
<div>
<label asp-for="workers">Workers</label>
<select multiple asp-for="workers" [email protected]>
<option value="">--Select--</option>
</select>
<span asp-validation-for="workers"></span>
</div>
}
</div>
I can pick and save workers, but when i'm trying to pick default option i have an error
CodePudding user response:
To use <option value="">--Select--</option>
, you need to make GUID field as nullable or else value in option tag cannot be null.
When you don't have nullable GUID, why do you even need a default option with null value, that does not even make any sense.
If you still want it as it is, you can try with default guid id i.e. 00000000-0000-0000-0000-000000000000