Why does form validation not work in MVC3 partial views?

Anybody? There is another question regarding this but the only answers were to code up some javascript validation, which also refuses to work on my partial view ("$ is not defined").

Anyway, I don't want to use javascript I just want simple validation for required fields that cannot be left blank, number fields that require ints, etc.

Can anyone shed some light on validation and partial views?

Answers


I suspect that you are loading those partial views using AJAX. If this is the case you will need to manually invoke the $.validator.unobtrusive.parse method once you inject the new contents of the partial into the DOM as explained in this article.

Brad Wilson also discussed this in his blog post:

The unobtrusive client validation script automatically parses the initial set of HTML for validation rules when the page has finished loading. If your page dynamically adds new HTML content (perhaps throught Ajax or through client-side application code), you may wish to parse that new HTML for client validation on the new HTML elements.

To parse new HTML, you can call the jQuery.validator.unobtrusive.parse() method, passing it a selector for the HTML that you would like to be parsed. You can also call the jQuery.validator.unobtrusive.parseElement() function to parse a single HTML element.

As far as the $ is not defined error you should make sure that you have included the proper scripts:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.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.min.js")" type="text/javascript"></script>

Also make sure you are not referencing any of the Microsoft*.js scripts. They are obsolete and should no longer be used in ASP.NET MVC 3.

Of course that's only a supposition, you haven't shown any code so we cannot know what you are doing.


Need Your Help

Loop through attribute of a class and get a count of how many properties that are not null

c#-4.0

I was wondering if there was a simpler way to do something like this?

How can I append a string to an incoming url when someone visits the root of my website?

.htaccess url arguments rewrite href

I'm trying to get the string /index.php?page=home behind the adress in de adressbar when people visit the root of my site.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.