I have tried everything to get client-side validation to work in MVC2 RC, but have been completely unsuccessful. Server-side validation works just fine, but client-side never fires.
I have grabbed
MicrosoftMvcJQueryValidation.js
from the futures source, since it wasn't included in RC. Unfortunately, that's the only script that people agree is necessary for everything to work. Besides that, nobody seems to know which other specific js scripts are needed. The
candidates are (jquery-1.3.2.js, jquery.validate.js, MicrosoftMvcAjax.js, MicrosoftMvcJQueryValidation.js). I've tried every permutation of these and other scripts to no avail.
Html.EnableClientValidation() has been included before BeginForm, and the JSON validation data appears to be correctly written into the source of the page. But, again, client-side validation never fires.
I'm hoping somebody has figured this out. Please help!
Yes, all the DataAnnotations are in place. For now, I am just using Required. As I mentioned, the server-side validation using this same setup is working perfectly. It's just the client-side validation that is never firing.
Since I posted my question, I found
this article from ScottGu written just 4 days ago. Everything is set up according to the article, but client validation is still not working. The only difference I can see is that he was using VS2010 and I am on VS2008 SP1. Although, it doesn't specify
whether he is using MVC2 RC or if he is describing changes that have happened since RC was released.
Thanks, ali62b. That doesn't work either. Unfortunately, the article you cited is outdated and not relevant to MVC2 RC. Trust me, I've tried every article that is out there, and none of them provide a solution. Every article lists different js scripts
to include. ScottGu's article I cited is the most recent one out there that I have found so far, but it's not helping.
I am curious to find out if anyone has actually gotten this to work (MVC2 RC on VS2008 3.5 SP1). I have not found adequate evidence to suggest that they have. If I can't find someone with first-hand experience with client-side validation working with this
particular setup, then I'll just give up on it and not burn time trying to make it work. But, if someone else has that experience, then I'll invest time troubleshooting.
Thanks yet again, ali62b. I've been trying to get this to work for so long, I didn't even think to create a brand new project and try it. Doh!
So, to mirror what you did, I tried that and it worked. The strange thing is that I used the same objects from my other solution which does not work. Same objects in different projects yield different results. Go figure. I guess something must have gotten
corrupted in the project somewhere, somehow.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<%= Html.ValidationSummary(true, "Edit was unsuccessful. Please correct the errors and try again.") %>
<%= Html.EditorForModel() %>
<input type="submit" />
<% } %>
</asp:Content>
My controller & model:
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public class Name {
[Required]
public string First { get; set; }
[Required]
public string Last { get; set; }
}
public class HomeController : Controller {
public ViewResult Index() {
return View(new Name());
}
[HttpPost]
public ActionResult Index(Name name) {
return View(name);
}
}
JSR_SDCA
Member
2 Points
15 Posts
MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to work...
Jan 19, 2010 08:21 AM|LINK
Hello all,
I have tried everything to get client-side validation to work in MVC2 RC, but have been completely unsuccessful. Server-side validation works just fine, but client-side never fires.
I have grabbed
from the futures source, since it wasn't included in RC. Unfortunately, that's the only script that people agree is necessary for everything to work. Besides that, nobody seems to know which other specific js scripts are needed. The candidates are (jquery-1.3.2.js, jquery.validate.js, MicrosoftMvcAjax.js, MicrosoftMvcJQueryValidation.js). I've tried every permutation of these and other scripts to no avail.Html.EnableClientValidation() has been included before BeginForm, and the JSON validation data appears to be correctly written into the source of the page. But, again, client-side validation never fires.
I'm hoping somebody has figured this out. Please help!
Thanks!!
vs2008 MVC RC1 client side validation .net 3.5 sp1
ignatandrei
All-Star
135087 Points
21669 Posts
Moderator
MVP
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 08:37 AM|LINK
Do you have DataAnnotation attribute such as Required ?
What validation on Business layer do you have?
JSR_SDCA
Member
2 Points
15 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 08:44 AM|LINK
Hello. Thanks for the reply.
Yes, all the DataAnnotations are in place. For now, I am just using Required. As I mentioned, the server-side validation using this same setup is working perfectly. It's just the client-side validation that is never firing.
Since I posted my question, I found this article from ScottGu written just 4 days ago. Everything is set up according to the article, but client validation is still not working. The only difference I can see is that he was using VS2010 and I am on VS2008 SP1. Although, it doesn't specify whether he is using MVC2 RC or if he is describing changes that have happened since RC was released.
ali62b
Contributor
4750 Points
690 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 08:49 AM|LINK
Make sure to add
<div> <div> </div></div><div> to your code .</div><div></div><div>http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx</div><div>
</div><div>Hope this helps.
</div>
@BlueCoder
Regards
JSR_SDCA
Member
2 Points
15 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 09:05 AM|LINK
Thanks, ali62b. That doesn't work either. Unfortunately, the article you cited is outdated and not relevant to MVC2 RC. Trust me, I've tried every article that is out there, and none of them provide a solution. Every article lists different js scripts to include. ScottGu's article I cited is the most recent one out there that I have found so far, but it's not helping.
I am curious to find out if anyone has actually gotten this to work (MVC2 RC on VS2008 3.5 SP1). I have not found adequate evidence to suggest that they have. If I can't find someone with first-hand experience with client-side validation working with this particular setup, then I'll just give up on it and not burn time trying to make it work. But, if someone else has that experience, then I'll invest time troubleshooting.
JSR_SDCA
Member
2 Points
15 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 09:07 AM|LINK
* Deleted double post *
ali62b
Contributor
4750 Points
690 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 09:10 AM|LINK
I created a project in VS 2008 SP1 with client validation and it worked the same project Scottgu done in VS 2010 .
But I think you want to use jQuery for your validation which I didn't test yet.
@BlueCoder
Regards
JSR_SDCA
Member
2 Points
15 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 09:40 AM|LINK
Thanks yet again, ali62b. I've been trying to get this to work for so long, I didn't even think to create a brand new project and try it. Doh!
So, to mirror what you did, I tried that and it worked. The strange thing is that I used the same objects from my other solution which does not work. Same objects in different projects yield different results. Go figure. I guess something must have gotten corrupted in the project somewhere, somehow.
At least I know it's possible to do.
Thanks very much!
JSR_SDCA
Member
2 Points
15 Posts
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 11:10 AM|LINK
OK. I figured out what the problem was. You're never gonna believe this! It turns out to be a very interesting and perplexing bug.
Long story short...
Your client-side validation will break (and sometimes lock up the browser) if the following conditions are true:
Steps to reproduce:
Weird, huh?
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: MVC2 RC on 3.5 SP1 : Client-Side Validation from DataAnnotations : Has anyone gotten this to ...
Jan 19, 2010 06:26 PM|LINK
I can't reproduce this with the RC.
My master page:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title> <style type="text/css"> .field-validation-valid, .validation-summary-valid { display: none; } .input-validation-valid- { border: solid 1px Black; } .field-validation-error, .validation-summary-errors { color: Red; font-weight: bold; } .input-validation-error { background-color: #ffcccc; border: solid 1px Red; } </style> <script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script> <script src="/Scripts/MicrosoftMvcValidation.debug.js" type="text/javascript"></script> <% Html.EnableClientValidation(); %> </head> <body> <table><tr><td> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </td></tr></table> </body> </html>My view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) { %> <%= Html.ValidationSummary(true, "Edit was unsuccessful. Please correct the errors and try again.") %> <%= Html.EditorForModel() %> <input type="submit" /> <% } %> </asp:Content>My controller & model:
using System.ComponentModel.DataAnnotations; using System.Web.Mvc; public class Name { [Required] public string First { get; set; } [Required] public string Last { get; set; } } public class HomeController : Controller { public ViewResult Index() { return View(new Name()); } [HttpPost] public ActionResult Index(Name name) { return View(name); } }