Can someone tell me why client side validation does not work in my MVC 4 application.
_layout.schtml
@Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false)
In my web.config, I have:
<appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
On my login.cshtml page, I have:
@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> }
At the bottom of the login page:
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
JavaScript is enabled in my browser. In the MVC 4 template from the client check, Visual Studio works fine.
Launching the application, on the login page when viewing the source of the page, I see that this is shown:
<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>
and below:
<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>
My model properties are annotated:
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 is a class derived from a regular RequiredAttribute. The reason is because my error messages are localized by overriding the FormatErrorMessage(string name) method of the RequiredAttribute class. And it works great. My tags and error messages are localized.
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; } }
I set a breakpoint in the POST version of my login action method, and it hits. The form is sent to the server, where the check is done on the server side. Client side validation does not occur.
What am I missing?
Thank.