The Experience ASP.NET MVC 3 Beta series
Part 1 In the coming ASP.NET MVC 3.0 a lot of new good things will be added or enhanced. In this article, we are going to focus upon the new view engine Razor to see how it will simplify the view design.
Part 2 In addition to the introduction of a new view engine Razor, ASP.NET MVC 3 Beta has also introduced numerous new HtmlHelpers, such as Chart, Crypto, WebGrid, WebImage, WebMail, etc. This article aims to introduce the two commonly-used new helper controls – WebGrid and Chart using relevant examples.
Part 3 In this installment, I'll first tell you a short story about unobtrusive JavaScript. Then, we'll delve into the unobtrusive client-side validation support. Finally, we will research into a more interesting story - the unobtrusive jQuery-Based Ajax support.
Part 4 In this article, we are going to continue to explore the other three important helpers - WebImage, WebMail, and Crypto.
Part 5 Starting from this article, let's explore some more advanced concepts and related utilizations associated with flexible Dependency Injection support introduced in ASP.NET MVC 3 Beta.
Part 6 In the last article, we've mainly discussed the new-styled DI support in ASP.NET MVC 3 Beta/RC in relation to the two new services - IControllerActivator and IViewPageActivator. Obviously, both of them are connected with controllers and views. In this article, however, we will shift our attention to the Model (generally called viewmodal in many blogs) related DI manipulations.
Part 7 Since ASP.NET MVC 1, CSRF (Cross Site Request Forgery) has been considered by introducing a set of anti-forgery helpers. In this article, we are to detail into CSRF related concepts and ASP.NET MVC's helper functions again CSRF.
Part 8 In this series of articles, we are going to explore as many as possible aspects of cache programming in the latest ASP.NET MVC 3 RC2 framework. And also, all the related samples have been tested against the latest ASP.NET MVC 3 RC 2.
Part 9 In the first part of this series, we've mainly explored Output Cache related issues. In this second part, however, we are going to delve into the general Data Cache topic.
Introduction
ASP.NET MVC 3.0 Beta has made a fundamental alteration to its Ajax and client validation code base, which is totally based on jQuery and its validation plug-in. With this change, ASP.NET MVC framework can benefit from the power of the jQuery library, enhance the performance and make it much easier to use. For example, with the unobtrusive JavaScript enabled, ASP.NET MVC 3.0 Beta framework can generate HTML 5 data- prefix attributes, decouples the scripts from the view content (ASPX or Razor).
In this installment, I'll first tell you a short story about unobtrusive JavaScript. Then, we'll delve into the unobtrusive client-side validation support. Finally, we will research into a more interesting story - the unobtrusive jQuery-Based Ajax support.
NOTEThe development environments we'll utilize in the sample application are:
1. Windows XP Professional (SP3);
2. .NET 4.0;
3. Visual Studio 2010;
4. ASP.NET MVC 3 Beta (http://www.microsoft.com/downloads/en/details.aspx?FamilyID=0abac7a3-b302-4644-bd43-febf300b2c51).
Introducing Unobtrusive JavaScript
According to Wikipedia, unobtrusive JavaScript is a general approach to the use of JavaScript in web pages. Though the term is not formally defined, its basic principles are generally around the following:
- Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
- Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
- Progressive enhancement to support user agents that may not support advanced JavaScript functionality
Advocates of Unobtrusive JavaScript see it as part of the larger Web standards movement; much as the demand for cross-browser compatibility has driven the increasing emphasis on standardized markup and style, the increasing demand for rich Internet applications is driving the movement toward better practices with the use of JavaScript. The concept of Unobtrusiveness in relation to JavaScript programming was coined in 2002 by Stuart Langridge in the article "Unobtrusive DHTML, and the power of unordered lists". In the article Langridge argues for a way to keep all JavaScript code, including event handlers, outside of the HTML.
Scraps of samples
Traditionally, JavaScript was often placed inline together with an HTML document's markup. For example, the following is a typical implementation of JavaScript form validation:
Adherents to "Unobtrusive Javascript" argue that the purpose of markup is to describe a document's structure, not its programmatic behavior and that combining the two negatively impacts a site's maintainability for similar reasons that combining content and presentation does. They also argue that inline event handlers are harder to use and maintain, when one needs to set handlers for several events on a single element, when one wants to set the same event handler on several elements, or when one is using event delegation. Nor can they be used with custom events.
The unobtrusive solution is to register the necessary event handlers programmatically, rather than inline. Rather than adding the onchange attribute explicitly as above, the relevant element(s) are simply identified, for example by class, id or some other means in the markup:
A script that runs when the page is first loaded into the browser can look for the relevant element(s) and set them up accordingly.
For example, using native JavaScript, HTML5 events and DOM Level 2 event listeners:
The above example will not work in all browsers; some – particularly Internet Explorer (including version 8) – do not support DOM 2 event listeners. In real scenarios, developers often use libraries to abstract away browser inconsistencies and deficiencies. For example, the following script is specific to the jQuery JavaScript library:
As it uses id, or other valid attributes and characteristics of the markup, this approach is consistent with modern standards-based markup practices.
By default, ASP.NET MVC 3 Beta uses jQuery validation in an unobtrusive manner in order to perform client-side validation. To gain more clarity and sharper contrast, let's first retrospect the traditional client-side validation.
Traditional Client-side Validation
First of all, we should notice that the client-side validation system since ASP.NET MVC 2.0 has been decoupled from the server-side validation system through the utilization of JSON. Compared with the server-side validation, the client-side validation is a pretty knotty one (which requires a better understanding with JavaScript programming). In this article, we will mainly focus upon the latter.
Set up Model
Start up Visual Studio 2010, select the "ASP.NET MVC 3 Web Application" template, and then select the project template "Internet Application" to create a project named Mvc3UnobtrusiveDemo.
For simplicity, we are going to create a very simple Model named ValidationModel, as shown below:
Above, with the help of C# data annotations, we've established the validation attributes for each related property. ASP.NET MVC 2 has built-in support for data annotation validation attributes for doing validation on a server. For details on how data annotations work with ASP.NET MVC 2, check out Brad’s blog post.
NOTE:Because users may have browser-side scripting disabled or intentionally try to send bad data to the server the server-side validation is a MUST HAVE. On the other hand, client-side validation has the benefit of giving the users quick feedback, as well as offloads some work from the server. In fact, the client-side validation is not a lone story because although the client-side validation system in MVC 3 Beta has been decoupled from the server-side one the former is still relevant to the latter. And, at the same time, we should clearly realize that to achieve the better client-side validation often requires you to be proficient in Javascript coding, especially as far as custom Ajax-based client-side validation is concerned.
Let's next set up the necessary action method to invoke our interested view, inside which the client-side validation will be performed.
Create the Action method
Right click the folder Controllers in the sample project Mvc3UnobtrusiveDemo and select "Add| Controller..." to add a controller named ValidationController. Then, create a simple action named TraditionalValidation, as illustrated below.
Above, we've built up a simple action TraditionalValidation with which to trigger the related view. Also, note the second action UnobtrusiveValidation will be used for test unobtrusive client-side validation later on.
Create the view
Now, right click the above action TraditionalValidation and from the shortcut menu select "Add View..." to create a new and strongly-typed view named TraditionalValidation, selecting the View engine as ASPX(C#). This view will serve two purposes: one is the traditional client-side validation; the other is unobtrusive jQuery-based client-side validation.
Well, let's take a look at the first case - the traditional client-side validation. Below gives the main coding.
First of all, we enabled the client-side validation support with the first line of code. Next, we turned off the unobtrusive JavaScript support. In this way, we will capture the same client-side markup output as ASP.NET MVC 2.0. And then, we set up a simple form with the strongly-typed class ValidationModel as the viewmodel.
Now, let's save up all newly-created files and press Ctrl+F5 to launch the sample application. Then, enter the url http://localhost:yourportnumber/Validation/TraditionalValidation and hit the ENTER key. If everything goes well, you will see something like Figure 1 below.
Figure 1: The traditional client-side validation in a MVC application

Till now, if you click "View |Source file" in your Internet Explorer, you will obtain the following client-side markup. For clarity, unimportant stuff has been omitted.
Have you noticed the second part starting from //< |
I'm a college teacher and also a freelance developer and writer from WeiFang China, with more than fourteen years of experience in design, and development of various kinds of products and applications on Windows platform. My expertise is in Visual C++/Basic/C#, SQL Server 2000/2005/2008, PHP+MyS...
This author has published 81 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Telerik Introduces Free Web Testing Framework for ASP.NET AJAX and Silverlight
read more
jQuery Intellisense in VS 2008
read more
Will ASP.NET MVC be the main web UI platform for ASP.NET?
read more
Download AjaxPro Beta with jQuery Support
read more
Using ASP.NET for AJAX in SharePoint Sites: Tread Gently for Now
read more
Sinking My Teeth Into ASP.NET AJAX 1.0 Beta 2
read more
ASP.NET AJAX 1.0 Beta 2 Release
read more
ComponentArt's Web.UI for ASP.NET AJAX
read more
Ajax.NET Professional 6.6.2.2 with new Converters
read more
Download latest Ajax.NET Professional 6.4.28.1
read more
|
|
Please login to rate or to leave a comment.