views:

242

answers:

2

I can't seem to get client side validation working with the version of MVC released with Visual Studio 2010 RC.

I've tried it with two projects -- one upgrade from 1.0, and one using the template that came with VS.

I'd think the template version would work, but it doesn't. Added the following scripts:

<script type="text/javascript" 
  src="<%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>">
</script>
<script type="text/javascript" 
  src="<%= Url.Content("~/Scripts/jquery.validate.js")%>">
</script>

which are downloaded to the client correctly. Added the following to my form page:

<% Html.EnableClientValidation(); %>
<%--yes, am aware of the EndForm() bug! --%>
<% using (Html.BeginForm()) { %>    
<%--snip --%>

and I can see the client validation scripts have been added to the bottom of the form. But still client validation never happens.

What is worse is that in my upgraded project, the client validation scripts are never output in the page!

PLEASE NOTE: I am specifically asking about the version of MVC2 that came with VS2010 RC. Also, I do know how to google; please don't waste anybody's time searching and answering if you aren't familiar with this issue in the release candidate of Visual Studio. Thanks.


Error ID10T: User editing one page and testing another.

However, I can't get client-side validation working on the MVC2 template project. If anybody has an idea how to, say, get it working for the registration page, please answer.

+1  A: 

I have had no luck getting this to work in MVC 2 RC. According to other questions here on SO, you have to get the MicrosoftMvcJQueryValidation.js file from the MVC Futures release, hold your left foot behind your head, and whistle Dixie for half an hour. I did this and more and have not been able to make it work.

I have, however, gotten it working by using the jQuery Validation plugin directly. More work, but it gets the job done.

Hopefully it will be fixed in RTM.

Dave Swersky
+3  A: 

I had the same problem also, the examples by MS(scottGu and haacked) are a bit confusing since you don't really know on which version they are talking about, and haacked updates his post every time a new version is out so you it's not relevant to you :(.

To make jQuery validation work On VS 2010 do the following:

  1. as you answered you need the MicrosoftMvcJQueryValidation.js file, several ways to get this file, i got it from the example project from haacked post: ASP.NET MVC 2 Custom Validation
  2. remove all JS references and leave just this one and the jquery.validate.min.js
  3. call <% Html.EnableClientValidation(); %> before the opening tag of the form
  4. Dont forget to put some Html.ValidationMessageFor(.. on your fields since this is the trigger for adding the enteries to the client validation JSON object

i guess you did all the above, but left the references to MicrosoftMvcValidation.js on the page, notice at the end of this file there is the hook to the MS client side validation.

funny i just wanted to blog about this issue this morning and found your question,\hope this helps

Avi Pinto