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)