ASP.NetCORE Razor 页面客户端验证未显示错误消息
ASP.NetCORE Razor page Client Side Validation not displaying error message
当提交的字段带有空字符串时,我无法获得客户端验证以显示 WebForm.FullName 的错误消息。当此字段为空时,它确实会阻止网页在提交时发布到服务器,所以我知道验证在某种程度上起作用了。关于未显示的错误消息,只是无法弄清楚我做错了什么。
到目前为止,我已经尝试过但没有成功:
- 将 jquery 验证器引用放在 WebForm.cshtml 页面中。
- 添加字段 ?标记,使它们在 WebForm.cs 中可以为空。
- 最后一次尝试是添加 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; }
}
我的同事帮我解决了这个问题:
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;">
当提交的字段带有空字符串时,我无法获得客户端验证以显示 WebForm.FullName 的错误消息。当此字段为空时,它确实会阻止网页在提交时发布到服务器,所以我知道验证在某种程度上起作用了。关于未显示的错误消息,只是无法弄清楚我做错了什么。
到目前为止,我已经尝试过但没有成功:
- 将 jquery 验证器引用放在 WebForm.cshtml 页面中。
- 添加字段 ?标记,使它们在 WebForm.cs 中可以为空。
- 最后一次尝试是添加 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; }
}
我的同事帮我解决了这个问题: 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;">