【发布时间】:2020-08-07 07:07:59
【问题描述】:
当使用空字符串提交该字段时,我无法让客户端验证显示 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; }
}
【问题讨论】:
标签: c# validation user-input razor-pages asp.net-core-3.1