“The value ” is invalid” 在可空字段中为什么是无效的?

huangapple go评论83阅读模式
英文:

Why "The value '' is invalid" in nullable field

问题

我有一个项目模型,其中包含一组可能为空的ID。

public class Project
{
[Required] [Key] public Guid id { set; get; }
...其他字段
public List? workers { set; get; }
}

我使用JsonConvert.Serialize将此列表存储在MySql数据库中。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(x =>
{
x.HasKey(y => y.id);
x.Property(y => y.workers).HasConversion(to => to == null ? null : JsonConvert.SerializeObject(to),
from => from == null ? new List() : JsonConvert.DeserializeObject<List>(from));
});
}

此外,我有一个用于编辑`Project`的表单,其中包含一个选择标签和`value=""`选项,用于表示空值。(@ViewBag.workers - SelectList)
@if (ViewBag.workers != null)
{



}

```
我可以选择和保存工作人员,但当我尝试选择默认选项时,出现错误。
```

英文:

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&lt;Guid&gt;? workers { set; get; }
}

I'm using JsonConvert.Serialize to store this list in MySql DB.

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity&lt;Project&gt;(x =&gt;
        {
            x.HasKey(y =&gt; y.id);
            x.Property(y =&gt; y.workers).HasConversion(to =&gt; to == null ? null : JsonConvert.SerializeObject(to),
                from =&gt; from == null ? new List&lt;Guid&gt;() : JsonConvert.DeserializeObject&lt;List&lt;Guid&gt;&gt;(from));
        });

Also i have an editing form for Project, where is select tag and option with value=&quot;&quot; for nullish value.(@ViewBag.workers - SelectList)

&lt;div class=&quot;div-box&quot;&gt;
@if (ViewBag.workers != null)
    {
        &lt;div&gt;
            &lt;label asp-for=&quot;workers&quot;&gt;Workers&lt;/label&gt;
            &lt;select multiple asp-for=&quot;workers&quot; asp-items=@ViewBag.workers&gt;
                &lt;option value=&quot;&quot;&gt;--Select--&lt;/option&gt;
            &lt;/select&gt;
            &lt;span asp-validation-for=&quot;workers&quot;&gt;&lt;/span&gt;
        &lt;/div&gt;
    }
&lt;/div&gt;

I can pick and save workers, but when i'm trying to pick default option i have an error

“The value ” is invalid” 在可空字段中为什么是无效的?

答案1

得分: 0

要使用&lt;option value=&quot;&quot;&gt;--选择--&lt;/option&gt;,您需要将GUID字段设置为可为空,否则选项标签中的值不能为null。
当您没有可为空的GUID时,为什么需要具有空值的默认选项,这甚至没有任何意义。

如果您仍然希望保持原样,可以尝试使用默认的GUID ID,即00000000-0000-0000-0000-000000000000

英文:

To use &lt;option value=&quot;&quot;&gt;--Select--&lt;/option&gt;, 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

huangapple
  • 本文由 发表于 2023年1月9日 16:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定