谁能告诉我为什么客户端验证在我的 MVC 4 应用程序中不起作用。
_layout.schtml
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
在我的 web.config 中,我有:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
在我的 login.cshtml 页面中,我有:
@using (Html.BeginForm())
{
<div class="formscontent">
<ol>
<li>
@Html.LabelFor(x => x.AgreementNumber)
<br />
@Html.TextBoxFor(x => x.AgreementNumber, new { size = "30" })
<br />
@Html.ValidationMessageFor(m => m.AgreementNumber)
<br />
<br />
</li>
<li>
@Html.LabelFor(x => x.UserName)
<br />
@Html.TextBoxFor(x => x.UserName, new { size = "30" })
<br />
@Html.ValidationMessageFor(m => m.UserName)
<br />
<br />
</li>
<li>
@Html.LabelFor(x => x.Password)
<br />
@Html.PasswordFor(x => x.Password, new { size = "30" })
<br />
@Html.ValidationMessageFor(m => m.Password)
<br />
<br />
</li>
</ol>
</div>
<ol>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<br />
<input class="mainbutton" type="submit" value="@Model.Localisation.TranslateHtmlString("LogonBtn")" /><br />
<div style="text-align: left; padding: 0 5px 5px 10px;">
Forgot login-info? clik <a class="link" href="@Url.Action("Index", "Credentials")">here.</a>
</div>
}
在登录页面底部:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我的浏览器中启用了 JavaScript。在 Visual Studio 客户端验证的 MVC 4 模板项目中工作正常。
运行应用程序,在查看页面源时在登录页面上,我看到这个呈现:
<label for="AgreementNumber">number</label>
<br />
<input id="AgreementNumber" name="AgreementNumber" size="30" type="text" value="387893" />
<br />
<span class="field-validation-valid" data-valmsg-for="AgreementNumber" data-valmsg- replace="true"></span>
在底部:
<script src="/BMWebsite/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/BMWebsite/Scripts/jquery.validate.inline.js"></script>
<script src="/BMWebsite/Scripts/jquery.validate.js"></script>
<script src="/BMWebsite/Scripts/jquery.validate.unobtrusive.js"></script>
我的模型属性已注释:
public class LogonModel : ModelBase
{
[MyRequired("AgreementNumberRequiredProperty")]
[MyDisplay("AgreementNumberLabel")]
public string AgreementNumber { get; set; }
[MyRequired("UserNameRequiredProperty")]
[MyDisplay("UserNameLabel")]
public string UserName { get; set; }
[MyRequired("PasswordRequiredProperty")]
[DataType(DataType.Password)]
[MyDisplay("PasswordLabel")]
public string Password { get; set; }
[MyDisplay("RememberMeCheckBox")]
public bool RememberMe { get; set; }
}
MyRequired
是从常规RequiredAttribute 派生的类。原因是我的错误消息是通过覆盖类的FormatErrorMessage(string name)
方法来本地化的RequiredAttribute
。它工作正常 - 我的标签和错误消息已本地化。
MyRequired.cs
public class MyRequiredAttribute : RequiredAttribute
{
private readonly string _errorMessagekey;
public MyRequiredAttribute(string errorMessage)
{
_errorMessagekey = errorMessage;
}
public override string FormatErrorMessage(string name)
{
var translation = HttpContext.Current.Session["translation"] as LocalisationHelper;
return translation != null ? translation.Translate(_errorMessagekey) : ErrorMessage;
}
}
我在我的登录操作方法的 POST 版本中放置了一个断点,它正在被命中。表单被发布到服务器端验证发生的服务器。客户端验证不会发生。
我错过了什么?
谢谢你。