ASP.MVC3 is client side validation enabled by defa

2019-09-03 06:37发布

问题:

In my Layout page, i have the following links to the validation Javascript files,

 <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"> </script>
         <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>

My entities are decorated with validation attributes like

[Required(ErrorMessage ="Please enter a customer name")]
    public string CustomerName { get; set; }

Then in my view i have validation messages specified after the update fields like so

<div class="label-for">@Html.LabelFor(model => model.CustomerName)</div>
<div class="editor">@Html.EditorFor(model => model.CustomerName)</div>
@Html.ValidationMessageFor(model => model.CustomerName) 

The validation is working however its not on the client side, the way i understand this is that error messages should be displayed when one of the fields is left blank afer tabbing to another field, is there anything else required to get client side validation working?

回答1:

IIRC there were some incompatibilities between jquery and jquery.validate versions. You seem to be using an old version of jquery 1.4.4. Try updating with the latest. For example install the ASP.NET MVC 3 Tools Update and create a new ASP.NET MVC project in Visual Studio which will get you the proper versions of the following scripts: jquery, jquery.validate and jquery.validate.unobtrusive.



回答2:

May be this work: Make sure that in your web.config file, you set "ClientValidationEnabled" to "True"

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
</appSettings>

NOTE: In MVC3 web project, there were two web.config files one under "View" folder another under root web project. make sure that the one in the root does not override the other.



回答3:

jQuery validation has nothing to do with your validation mechanism. Maybe Microsoft.Ajax.Validation should be the responsible. But to use jquery.validation, you should write code. That's all. It doesn't do anything out of the box.