【问题标题】:ASP.NetCORE Razor page Client Side Validation not displaying error messageASP.Net CORE Razor 页面客户端验证不显示错误消息
【发布时间】:2020-08-07 07:07:59
【问题描述】:

当使用空字符串提交该字段时,我无法让客户端验证显示 WebForm.FullName 的错误消息。当此字段为空时,它确实会阻止网页在提交时发布到服务器,所以我知道验证有点工作。只是无法弄清楚我在不显示错误消息方面做错了什么。

到目前为止,我已经尝试过没有成功:

  1. 将 jquery 验证器引用放在 WebForm.cshtml 页面中。
  2. 使用 ? 添加字段标记,使它们在 WebForm.cs 中可以为空。
  3. 最后一次尝试是添加 z-index。

任何帮助将不胜感激。提前谢谢你。

WebForm.cshtml

    <div>
    <form method="post">
        <div class="w-75" style="margin:0 auto; border: 1px solid lightgrey; padding:20px;">
            <div class="row justify-content-center">
                <div class="form-group col-md-4" style="padding:5px;">
                    <label for="WebForm.FullName">Name:</label>
                    <input asp-for="WebForm.FullName" name="FullName" size="25" style="text-align:center;">
                    <span asp-validation-for="WebForm.FullName" class="text-danger" style="z-index:999;"></span>
                </div>
            </div>
            <div class="row justify-content-end">
                <div class="form-group" style="padding:5px;">
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </div>
    </form>
    </div>

WebForm.cshtml.cs

    public class WebFormModel : PageModel
{
    //This is the Data Binding model class. 
    [BindProperty]
    public WebForm WebForm { get; set; }

    public void OnGet()
    {

    }

    //Handle form post here. 
    public void OnPost(WebForm WebForm)
    { 

    }
}

WebForm.cs

    public class WebForm
{
    [Required(ErrorMessage="Full name is required.")]
    public string FullName { get; set; }
}

【问题讨论】:

    标签: c# validation user-input razor-pages asp.net-core-3.1


    【解决方案1】:

    我的同事帮我解决了这个问题: WebForm.cshtml文件中的这一行html

        <input asp-for="WebForm.FullName" name="FullName" size="25" style="text-align:center;">
    

    应该是:

        <input asp-for="WebForm.FullName" name="WebForm.FullName" size="25" style="text-align:center;">
    

    【讨论】:

    • 仅供参考:您无需手动设置名称,它会为您设置。
    • 这个很高兴知道以供将来参考,谢谢
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2012-08-24
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多