While doing a sample using MVC3 razor, I wrote:
<p>
Show me the time in:
@Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })
@Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" })
@Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" })
</p>
<div id="myResults" style="border: 2px dotted red; padding: .5em;">
Results will appear here
</div>
<p>
This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC)
</p>
None of my ajax calls worked until I changed this key in web.config:
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
I read in this article: http://weblogs.asp.net/owscott/archive/2010/11/17/mvc-3-ajax-redirecting-instead-of-updating-div.aspx
But now my client-side validation is not working as before.
My question is: how do I make both the ajax and client-side validations work at the same time? What does the “UnobtrusiveJavaScriptEnabled” do? Is it a switch between them?! I hope to understand more about it in simple terms.
In ASP.NET MVC 3 there are 2 things: client side validation and unobtrusive javascript which are controlled by their corresponding values in web.config:
Client side validation is based on the
jquery.validate.jsplugin alongside with thejquery.validate.unobtrusive.jsscript from Microsoft. When you include those two scripts inside a view which contains a HTML form client side validation will be performed based on the data annotation rules you have defined on your model. When you look at the generated HTML source code of the view you will notice that input fields have HTML5data-*attributes which contain the validation rules. The Microsoft unobtrusive validation script would then read those rules and configure the jquery validate plugin.The unobtrusive javascript is different. It is based on jquery. When you use one of the
Ajax.*HTML helpers such asAjax.ActionLink, in ASP.NET MVC 3, those helpers also emit HTML5data-*attributes on the corresponding anchor. Those attributes are then interpreted by the Microsoftjquery.unobtrusive-ajax.jsscript which you need to include in your page and AJAXify those links. So for example when you write:this would generate the following HTML:
As you can see now all the information about how to perform the AJAX request is contained in the DOM. So you could have a separate javascript file where you would subscribe for the
clickevent of this link, send an AJAX request to the url contained in thehrefattribute and then based on the value of thedata-ajax-modeattribute replace the html of some container with id contained in thedata-ajax-updateattribute selector. And that’s exactly what thejquery.unobtrusive-ajax.jsdoes. It’s just that it is in a separate file and your markup and javascript are independent which wasn’t the case in previous versions.So contrary to ASP.NET MVC 1 and 2, in ASP.NET MVC 3 jQuery is the default javascript framework and HTML helpers are based on it. All
MicrosoftAjax*scripts are no longer used.