ASP.NetCORE Razor页面客户端验证不显示错误消息

时间:2020-04-23 20:04:14

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

当使用空字符串提交该字段时,我无法获得客户端验证以显示WebForm.FullName的错误消息。当此字段为空时,它的确阻止了网页在提交时发布到服务器上,因此我知道验证在某种程度上起作用。只是无法弄清楚关于未显示错误消息的问题。

到目前为止,我一直没有成功:

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

任何帮助将不胜感激。预先谢谢你。

enter image description here

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; }
}

1 个答案:

答案 0 :(得分:0)

我的同事帮我弄清楚了这个问题: 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;">