相关疑难解决方法(0)

ModelState 错误:值“null”对于可为空的字段无效

ModelState 会抛出错误,因为可空字段为空。

我有一个模型:

public class PersonModel
{
    public int? ID { get; set; }

    [Required]
    [StringLength(256)]
    public string Title { get; set; }

    [Required]
    [StringLength(256)]
    public string Name { get; set; }

    [Required]
    [StringLength(256)]
    public string Lastname { get; set; }

    [StringLength(1024)]
    public string Description { get; set; }

    public int? OrganizationID { get; set; }

    public string Organization { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

控制器:

var errors = ModelState.Where (c => c.Value.Errors.Count > 0).Select (c => c.Value).ToList ();

if (!errors.Any …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-core

7
推荐指数
1
解决办法
6459
查看次数

标签 统计

asp.net-core ×1

asp.net-mvc ×1

c# ×1