I was doing a sample using MVC3 razor, and wrote this:
<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>
All my ajax calls didn't work until i changed this key in web.config:
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
That What i read about in this article: http://weblogs.asp.net/owscott/archive/2010/11/17/mvc-3-ajax-redirecting-instead-of-updating-div.aspx
But now all my client cide validation is not working as before.
My question is: how to make the ajax code work and the client side validations in the same time? and what is this property about "UnobtrusiveJavaScriptEnabled"? is it a switch between them?! I hope to understand more about it in simple words.
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.js
plugin alongside with thejquery.validate.unobtrusive.js
script 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.js
script 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
click
event of this link, send an AJAX request to the url contained in thehref
attribute and then based on the value of thedata-ajax-mode
attribute replace the html of some container with id contained in thedata-ajax-update
attribute selector. And that's exactly what thejquery.unobtrusive-ajax.js
does. 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.I resolved that using this code:
Adding the lines:
You're adding the same behavior than the
Ajax.BeginForm
without loses the javascript behavior.I tested that, with
MVC4