我有一个简单的 ASP.NET MVC 表单,一切正常,唯一的事情是我想在选择字段时自动显示验证消息。但要显示消息验证,我需要发送表单。我需要使用 ajax 吗?谢谢你的帮助。
这是我的表格:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "TestForm" }))
{
<div>
@Html.LabelFor(m => m.FirstName)
</div>
<div>
@Html.TextBoxFor(m => m.FirstName, new { id = "FirstName" })
@Html.ValidationMessageFor(m => m.FirstName)
</div>
<div>
@Html.LabelFor(m => m.LastName)
</div>
<div>
@Html.TextBoxFor(m => m.LastName, new { id = "LastName" })
@Html.ValidationMessageFor(m => m.LastName)
</div>
...
}
模型
public class TestModel
{
[Required(ErrorMessage = "FirstName should blabla")]
[StringLength(4, ErrorMessage = "You must...", MinimumLength = 1)]
[DataType(DataType.Text)]
[Display(Name = "FirstName")]
public string FirstName { get; set; }
[Required(ErrorMessage = "LastName should blabla")]
[StringLength(4, ErrorMessage = "You must...", MinimumLength = 1)]
[DataType(DataType.Text)]
[Display(Name = "LastName")]
public string LastName { get; set; }
...
}