ASP验证 - 萨默里在模型模式下未显示错误

发布于 2025-02-02 02:52:03 字数 2130 浏览 2 评论 0原文

我已经注意到,当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文