我正在尝试验证此模型:
public class LogonModel
{
[Required(ErrorMessage="Username is required")]
public string Username { get; set; }
[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
}
在此行动中:
public ActionResult Logon()
{
LogonModel model = new LogonModel();
return View(model);
}
在此视图中:
@model POCModelValidation.Models.LogonModel
@{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>Index</h2>
@using (Html.BeginForm())
{
<h3>username</h3>
@Html.EditorFor(model => model.Username)
<h3>@Html.ValidationMessageFor(model => model.Username)</h3>
<br />
<h3>Password</h3>
@Html.EditorFor(model => model.Password)
<h3>@Html.ValidationMessageFor(model => model.Password)</h3>
<br />
<h3>Email</h3>
@Html.EditorFor(model => model.Email)
<h3>@Html.ValidationMessageFor(model => model.Email)</h3>
<input type="submit" value="Submit"/>
}
..我无法弄清楚它的作用模式,但它从不适用于整个3个领域。除此之外,如果我填写,离开,然后返回并删除字段的内容足够多次,它最终适用于所有这些,但很少在第一次,并且永远不会同时为所有这些。<登记/>
我的web.config
...
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
...
在_Layout中:
...
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
...
有什么想法吗?
答案 0 :(得分:0)
加载Fiddler http://www.fiddler2.com/fiddler2/并确保脚本正确加载。如果不是,验证就不起作用。
答案 1 :(得分:0)
请在代码中添加以下行
<%Html.EnableClientValidation();%>
@using (Html.BeginForm())
**Your code**