为什么页面加载后就直接显示了验证信息,都还没开始输入邮箱
Html代码如下:
@model Person @{ Layout = null; } <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> <form > @Html.TextBoxFor(m => m.Email) @Html.ValidationMessageFor(m => m.Email) <button id="btn">提交</button> </form>
后台代码如下:
namespace MVCEasyUI.Controllers { public class HomeController : Controller { public ActionResult FormValid(Person person) { return View(person); } } } public class Person { [Required(ErrorMessage = "{0}必填")] [EmailAddress(ErrorMessage = "邮箱格式不正确")] [Display(Name = "邮件")] public string Email { get; set; } }
在return View(person);之前做ModelState.Clear();试试,应该是可以的。具体原因我记不太清楚了。