Error in jquery.validate.js in MVC 4 Project with jQuery 1.9

This issue is fixed in jQuery Validation Plugin 1.11.0pre. Unfortunately there is currently no pre-release build on NuGet, so it is currently necessary to download jquery.validation.js directly from GitHub: jQuery.Validation 1.11 is now available via NuGet (thanks @Simon_Weaver). UPDATE It looks like general support for jQuery 1.9 is in the jQuery.Validation code base but not …

Read more

Revalidating a modified ViewModel within a controller method?

Once you have removed the offending item(s), clear the ModelState and validate again, like so: ModelState.Clear(); TryValidateModel(crew); // assumes the model being passed is named “crew” Note: Be carefull when use TryValidateModel method because this method does not validate nested object of model (As mentioned by @Merenzo).

MVC 4 client side validation not working

I had the same problem. It seems that the unobtrusive validation scripts were not loaded (see screenshot at the end). I fixed it by adding at the end of _Layout.cshtml @Scripts.Render(“~/bundles/jqueryval”) The end result: @Scripts.Render(“~/bundles/jquery”) @Scripts.Render(“~/bundles/jqueryval”) @RenderSection(“scripts”, required: false) Except for my pretty standard CRUD views everything is Visual studio project template defaults. Loaded scripts …

Read more

How to change ‘data-val-number’ message validation in MVC while it is generated by @Html helper

You can override the message by supplying the data-val-number attribute yourself when rendering the field. This overrides the default message. This works at least with MVC 4. @Html.EditorFor(model => model.MyNumberField, new { data_val_number=”Supply an integer, dude!” }) Remember that you have to use underscore in the attribute name for Razor to accept your attribute.

jquery.validate.unobtrusive not working with dynamic injected elements

I tried Xhalent’s approach but unfortunately it wasn’t working for me. Robin’s approach did work and didn’t work. It worked great for dynamically added elements, but if you tried to use JQuery to remove all the validation attributes and spans from the DOM, the validation library still would try to validate them. However, if you …

Read more

unobtrusive validation not working with dynamic content

If you try to parse a form that is already parsed it won’t update What you could do when you add dynamic element to the form is either You could remove the form’s validation and re validate it like this: var form = $(formSelector) .removeData(“validator”) /* added by the raw jquery.validate plugin */ .removeData(“unobtrusiveValidation”); /* …

Read more

What is the best way of adding a greater than 0 validator on the client-side using MVC and data annotation?

You can’t store a number bigger than what your underlying data type could hold so that fact that the Range attribute requires a max value is a very good thing. Remember that ∞ doesn’t exist in the real world, so the following should work: [Range(1, int.MaxValue, ErrorMessage = “Please enter a value bigger than {1}”)] …

Read more