当使用空字符串提交该字段时,我无法获得客户端验证以显示WebForm.FullName的错误消息。当此字段为空时,它的确阻止了网页在提交时发布到服务器上,因此我知道验证在某种程度上起作用。只是无法弄清楚关于未显示错误消息的问题。
到目前为止,我一直没有成功:
任何帮助将不胜感激。预先谢谢你。
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; }
}
答案 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;">