ASP验证 - 萨默里在模型模式下未显示错误
我已经注意到,当asp-validation-summary =“ modelonly”
时,未显示错误。当我将值更改为asp-validation-summary =“ all”
时,它可以正常工作。
也许是因为我正在使用验证attribute
? 这是代码样本:
@page "/pages/test"
@model TestPageModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, WebApp
@using Microsoft.AspNetCore.Mvc.ModelBinding
@using Microsoft.AspNetCore.Mvc.RazorPages
@using System.ComponentModel.DataAnnotations
<!DOCTYPE html>
<html>
<head>
<link href="~/lib/twitter-bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="m-2">
<h5 class="bg-primary text-white text-center p-2">TEST</h5>
<form asp-page="TestPage" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label>Name</label>
<div><span asp-validation-for="TestModel.Value" class="text-danger"></span></div>
<input class="form-control" asp-for="TestModel.Value" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
@functions{
public class TestPageModel : PageModel
{
[BindProperty]
public TestModel TestModel { get; set; } = new TestModel();
public Task OnGetAsync()
{
TestModel = new TestModel() { Value = "Some value" };
return Task.CompletedTask;
}
public IActionResult OnPost() => Page();
}
public class TestValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
{
return new ValidationResult("Something is wrong");
}
}
[TestValidation]
public class TestModel
{
public string Value { get; set; } = string.Empty;
}
}
您能帮我找到缺少的东西吗?
I have noticed that errors are not shown when asp-validation-summary="ModelOnly"
. When I change value to asp-validation-summary="All"
it works fine.
Maybe it is because I am using ValidationAttribute
?
Here is code sample:
@page "/pages/test"
@model TestPageModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, WebApp
@using Microsoft.AspNetCore.Mvc.ModelBinding
@using Microsoft.AspNetCore.Mvc.RazorPages
@using System.ComponentModel.DataAnnotations
<!DOCTYPE html>
<html>
<head>
<link href="~/lib/twitter-bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="m-2">
<h5 class="bg-primary text-white text-center p-2">TEST</h5>
<form asp-page="TestPage" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label>Name</label>
<div><span asp-validation-for="TestModel.Value" class="text-danger"></span></div>
<input class="form-control" asp-for="TestModel.Value" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
@functions{
public class TestPageModel : PageModel
{
[BindProperty]
public TestModel TestModel { get; set; } = new TestModel();
public Task OnGetAsync()
{
TestModel = new TestModel() { Value = "Some value" };
return Task.CompletedTask;
}
public IActionResult OnPost() => Page();
}
public class TestValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
{
return new ValidationResult("Something is wrong");
}
}
[TestValidation]
public class TestModel
{
public string Value { get; set; } = string.Empty;
}
}
Could you please help me find what is missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论