在模型类中,设置Title为Required,Body未设置。但是在提交表单时,Title和Body都必须填写,否则验证不通过,提示:
The Title field is required.
The Body field is required.
模型类:
public class FormTest
{
public int Id { get; set; }
[Required]
public string Title{ get; set; }
public string Body{ get; set; }
}
视图类:
@model WebSite.Models.FormTest
<form method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<input asp-for="Title" />
<input asp-for="Body" />
<button type="submit">submit</button>
</form>
控制器:
public IActionResult Cs()
{
return View();
}
[HttpPost]
public IActionResult Cs(Models.FormTest model)
{
if (ModelState.IsValid)
{
return Content("ok");
}
return View(model);
}