为什么 ModelState.IsValid 没有 [Required] 检查模型属性?
Why does ModelState.IsValid check the model properties without [Required]?
我是 ASP 的新手,在 ASP.NET Core (6.0) razorpage 项目中,我发现 ModelState.IsValid
会检查模型的所有属性。比如我有一个模型:
public class SimpleModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int Age { get; set; }
}
和一个页面Example.cshtml
,格式为:
@page "/example"
@model StudentManagement.Pages.ExampleModel
@{
ViewData["Title"] = "Example";
}
<form method="post" class="mt-3">
<input hidden asp-for="simpleModel.Id" />
<div class="form-group row">
<label asp-for="simpleModel.Name" class="col-sm-2 col-form-label">
</label>
<div class="col-sm-10">
<input asp-for="simpleModel.Name" class="form-control" placeholder="Name">
<span asp-validation-for="simpleModel.Name"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="simpleModel.Age" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="simpleModel.Age" class="form-control" placeholder="Age">
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
Example.cshtml.cs
:
public class ExampleModel : PageModel
{
public SimpleModel simpleModel { get; set; }
public ExampleModel()
{
simpleModel = new SimpleModel()
{
Id = 1,
Name = "Tom",
Age = 15
};
}
public void OnGet()
{
}
public IActionResult OnPost(SimpleModel simpleModel)
{
if (ModelState.IsValid)
{
this.simpleModel.Age = simpleModel.Age;
this.simpleModel.Name = simpleModel.Name;
}
return Page();
}
}
问题是当单击带有空白 Age
的 Update
时,ModelState.IsValid
是错误的。为什么 ModelSate
即使没有 [Required] 也不会忽略 Age
?
我试过用int? Age
和ModelState.IsValid
return是的,我还是想知道它是如何工作的。提前致谢。
ModelState.IsValid
表示是否可以将来自请求的传入值正确绑定到模型,以及在模型绑定过程中是否违反了任何明确指定的验证规则。
官方关于non-nullable properties or parameters
的解释如下
The validation system treats non-nullable parameters or bound
properties as if they had a [Required(AllowEmptyStrings = true)]
attribute. By enabling Nullable contexts, MVC implicitly starts
validating non-nullable properties or parameters as if they had been
attributed with the [Required(AllowEmptyStrings = true)]
attribute.
If the app was built with <Nullable>enable</Nullable>
, a missing
value for Name in a JSON or form post results in a validation error.
Use a nullable reference type to allow null or missing values to be
specified for the Name property:
您可以参考这篇link了解更多Model validation
我是 ASP 的新手,在 ASP.NET Core (6.0) razorpage 项目中,我发现 ModelState.IsValid
会检查模型的所有属性。比如我有一个模型:
public class SimpleModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int Age { get; set; }
}
和一个页面Example.cshtml
,格式为:
@page "/example"
@model StudentManagement.Pages.ExampleModel
@{
ViewData["Title"] = "Example";
}
<form method="post" class="mt-3">
<input hidden asp-for="simpleModel.Id" />
<div class="form-group row">
<label asp-for="simpleModel.Name" class="col-sm-2 col-form-label">
</label>
<div class="col-sm-10">
<input asp-for="simpleModel.Name" class="form-control" placeholder="Name">
<span asp-validation-for="simpleModel.Name"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="simpleModel.Age" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="simpleModel.Age" class="form-control" placeholder="Age">
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
Example.cshtml.cs
:
public class ExampleModel : PageModel
{
public SimpleModel simpleModel { get; set; }
public ExampleModel()
{
simpleModel = new SimpleModel()
{
Id = 1,
Name = "Tom",
Age = 15
};
}
public void OnGet()
{
}
public IActionResult OnPost(SimpleModel simpleModel)
{
if (ModelState.IsValid)
{
this.simpleModel.Age = simpleModel.Age;
this.simpleModel.Name = simpleModel.Name;
}
return Page();
}
}
问题是当单击带有空白 Age
的 Update
时,ModelState.IsValid
是错误的。为什么 ModelSate
即使没有 [Required] 也不会忽略 Age
?
我试过用int? Age
和ModelState.IsValid
return是的,我还是想知道它是如何工作的。提前致谢。
ModelState.IsValid
表示是否可以将来自请求的传入值正确绑定到模型,以及在模型绑定过程中是否违反了任何明确指定的验证规则。
官方关于non-nullable properties or parameters
的解释如下
The validation system treats non-nullable parameters or bound properties as if they had a
[Required(AllowEmptyStrings = true)]
attribute. By enabling Nullable contexts, MVC implicitly starts validating non-nullable properties or parameters as if they had been attributed with the[Required(AllowEmptyStrings = true)]
attribute.If the app was built with
<Nullable>enable</Nullable>
, a missing value for Name in a JSON or form post results in a validation error. Use a nullable reference type to allow null or missing values to be specified for the Name property:
您可以参考这篇link了解更多Model validation