How to determine why unobtrusive validation is not working - asp.net-mvc-3

Is there a way to trace why unobtrusive validation is not working?
I have disabled all my custom validators, and just using MVC validators, and still it posts back to the server for validation when I click submit.
It's driving me nuts. The only thing i can think of is that i have a hidden input that has no validation attributes on it other than [HiddenInput(DisplayValue = false)].
Is there a specific javascript function i can put a breakpoint in to determine which field is causing the form to submit?
EDIT:
I just realize that only IE8 is causing the server side validation. IE9, FF, and Chrome all are fine.
EDIT 2:
Ok, it must have been a cache issue or something. At some point it started working again, and it's still working even after restoring everythng.
Lesson learned. Alwasy clear you cache when you get goofy problems.

Make sure you have reference the following scripts and that the path is correct:
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
and that you have enabled client side validation in your web.config:
<add key="ClientValidationEnabled" value="true" />
Another important aspect is that if the form has been injected into the DOM as a result of an AJAX call, unobtrusive validation will not be attached to its elements and it will not work. You will have to attach it manually by using the $.validator.unobtrusive.parse method in your AJAX success callback after injecting it into the DOM.
Also FireBug's console tab is might be useful in showing potential javascript errors with your code.

Related

MVC3 Cannot attach jquery.unobtrusive-ajax.min.js file only

I am using ajax controls. When I attach jquery.unobtrusive-ajax.min.js file only I am getting "Error: 'jQuery' is undefined". But when I attach jquery-1.4.4.min.js file too, everything is ok. Why?
Make sure you load it as follows:
<script src="#Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
It uses jQuery that is why it is needed. Plugins are built on top of jQuery and uses jQuery as the base. That is why it is needed on every page that has a jQuery plugin.
It's like building a house with no foundation. What will happen? The house will fall apart because there is nothing that it can stand on. The same scenario applies here.
because it uses jQuery... so you must include jQuery!
you need to include the reference to jQuery library for other jQuery plugins to work. Without jQuery present in your page,you can not run any jQuery plugins.

How to scrape ajax generated content from JSF-Site?

I am currently playing around with different scraping techniques and found out, that it can get pretty complicated quickly when a lot of javascript is involved.
I had some success with HTMLUnit which seems to interpret javascript rather well, but I am looking for a more lightweight solution.
So the problem I am facing now is: I want to retrieve the results of a specific page, which is generated by an ajax call by a click on a certain button.
The call itself is rather simple, just a HTTP Post to a certain URL with a few parameters submitted in the post body. The problem I have now is that the server complains when I submit the HTTP Post to the ajax function without really opening the containing site.
What I basically do for testing is:
curl -v -d "AJAXREQUEST=..." https://myhost/ajaxurl
An what I get is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Ajax-Response" content="true" />
<meta name="Ajax-Expired" content="View state could't be restored - reload page ?" />
</head>
</html>
The server is running JSF 1.2. What do I have to do, to get the results from the AJAX call? I am not really a JSF expert...
If I had to guess, JSF doesn't have a session associated with the request being sent with curl and therefore the objects associated with the page don't exist. For curl look at http://curl.haxx.se/docs/httpscripting.html section 10, cookies. You would have to pull the page, get the cookies then do the http post with the cookies (starts being a lot of work with curl).
However I would instead suggest looking at Selenium, which has a IDE that generates Java to interact with JavaScript.

JSF 2.0 Ajax and javascript reloading

I use JSF Ajax on my menu links to load the content of the page.
content is loaded with ui:insert and ui:composition with template attribute.
ajax is work and load the content with no reloadin the all page but I have a problem with my java scripts.
when I click menu Items the script reload with every new ajax request.
what should I do to prevent script reloading?
I'm not sure I understood your problem clearly, but maybe you should try to play with the rendered attribute of the h:outputScript component, for instance:
<h:outputScript library="js" name="ajax-util.js"
rendered="#{myBean.renderAjaxFunctions}" />

ASP.MVC3 is client side validation enabled by default?

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?
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.
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.
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.

Ajax update in a Sharepoint 2010 Webpart

Is there any easy way to make a Ajax call in a Webpart?
I have a Webpart with a button, and I want that when the user pushes the button, it executes a server function without reloading the page. And then, if all is ok, execute a callback function. I thought that the best way is with a AJAX call.
But when I've looked for how to do it I only get some complicated tutorials that I don't really understand (and most are from old versions of Sharepoint). Any help? What is the best way to start? Thanks
Have you tried using jQuery with a content editor web part? I've done this before and it's rather easy. Here is a step by step for how I do it.
Download jQuery.
Upload jQuery to SiteAssets in Sharepoint.
Upload coded file (see below) with AJAX calls.
Point to coded file via Content Editor Web Part.
It should work!
Here is a default way something should work.
<html>
<head>
<script src="<point to jquery file>"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#main').load('<RELATIVE URL TO SERVER PAGE>');
});
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>
If possible I would go for a Visual Web Part in Visual Studio. You basically create a .NET user control. It saves you a lot of manual control definitions etc.
If in SharePoint2007 you might want to take a look at the "Smartpart". It has ajax support and some great tutorials on how to use it.

Resources