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:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Client side validation is based on the jquery.validate.js
plugin alongside with the jquery.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 HTML5 data-*
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 as Ajax.ActionLink
, in ASP.NET MVC 3, those helpers also emit HTML5 data-*
attributes on the corresponding anchor. Those attributes are then interpreted by the Microsoft jquery.unobtrusive-ajax.js
script which you need to include in your page and AJAXify those links. So for example when you write:
@Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })
this would generate the following HTML:
<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#myResults" href="/Home/GetTime?zone=utc">UTC</a>
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 the href
attribute and then based on the value of the data-ajax-mode
attribute replace the html of some container with id contained in the data-ajax-update
attribute selector. And that's exactly what the jquery.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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…