ASP. net mvc V2 Preview 1 release expectation VS better performance

Source: Internet
Author: User
Tags form post list of attributes

ASP. net mvc V2 Preview 1 official homepage: http://aspnet.codeplex.com/can be downloaded here

The following is a repost from a netizen, which is detailed:

The preview version is in. NET 3.5 SP1 and VS 2008, which can work with ASP. net mvc 1.0 is installed on the same machine in parallel (that is, they do not conflict with each other. If you install 2.0, your existing ASP. net mvc 1.0 project will not be affected ). If you install ASP. net mvc 1.0 and ASP. net mvc 2.0 at the same time, you will see two ASP. net mvc project templates in the "New Project" dialog box of Visual Studio 2008:

DataAnnotation (data tag) Verification supported

ASP. net mvc 2 now includes. NET 3.5 SP1 built-in support for DataAnnotation verification, which is provided in ASP. NET Dynamic Data and.. net ria service. DataAnnotation provides a simple way to add verification rules to the Model and ViewModel classes in the application in a declarative manner. In ASP. net mvc supports automatic binding and UI-assisted method verification.

To learn how to use this feature in practice, we can create a new "Customer" view model class as follows, contains five attributes (implemented using one of the C # features -- Automatic attributes ).

Then, we can use the DataAnnotation feature in the System. ComponentModel. DataAnnotations namespace to describe the appropriate verification rules on these attributes. The following code uses four built-in verification rules: Required], [StringLength], [Range], and [RegularExpression]. The namespace also contains a base class ValidationAttribute, which you can inherit to create your own custom verification features.

Then, we can Create a CustomersController class with two Create action methods. The first Create action method processes http get requests corresponding to the "/Mers MERs/Create" URL, and displays a view template based on a blank Customer object. The second Create action method processes the http post request of the same URL and accepts a Customer object as the method parameter. It checks whether submitted input data has caused any model binding error. If an error occurs, it uses the input data to re-display the view template. If no error occurs, it will show you a view that has been submitted successfully:

The release notes of ASP. net mvc 2 preview details how to upgrade existing ASP. net mvc 1.0 projects to V2 if you want to port them to use the new features of V2.

New Features

ASP. net mvc V2 will include a bunch of new features and features (some of them are listed on the ASP. net mvc product roadmap page ). Some new features included in today's "first preview" are the first to show up. More features will be available in future previews. The code for the first preview version also belongs to an earlier version. The Development Team released this version today and intends to seek your feedback and integrate it into future versions.

The following briefly describes some of the new features:

 Regional support

ASP. net mvc 2 will support a new feature called "region", allowing you to easily split and combine the functions of MVC applications.

"Region" provides a way to group controllers and views, allowing you to build the sub-parts of a large application relatively independently. Each region can be implemented in the form of a separate ASP. net mvc project, and these projects can be further referenced by the main application. This helps to manage the complexity of a large application and facilitates the development of applications by multiple teams.

The following screen shows three projects in a single solution. One of these projects is "CompanySite", which includes the core website content, layout, and related controllers and views. There are two separate "region" projects: "Blogs" (blog) and "Forums" (forum ). These two projects implement the functions under the/Blogs and/Forums URLs of the website, encapsulate all the path rules, and implement the controllers and views of these two parts:

The first preview only includes the first part of regional feature implementation and does not support any tools. (currently, you can only manually add compilation tasks to create and configure regional projects ). In the future, the preview version will include tool support and further expand and improve the feature set.

Finally, we can right-click any of the above Create action methods and select the "add view" context menu command to automatically generate (scaffold) A framework based on the "Create" view template of the Customer object. In this case, the generated framework view template contains the following HTML <form>:

In this way, when we request the "/Mers MERs/Create" URL in the browser, we will get a blank form starting like below:

If we enter invalid data, ASP. net mvc 2 Model binder will detect that our Customer class has the DataAnnotations feature and will automatically use them to verify the submitted form input data. If there is an error, our controller action method will re-display the form and display the appropriate verification error message to the user, as shown below. Note: How can we use the verification attribute error message string specified by the DataAnnotation feature to display it to the user through the Html. Validation auxiliary method without writing additional code.

The above form will redisplay with error messages each time the user enters invalid input and attempts to perform a form post.

In the future, ASP. in the. net mvc 2 preview version, we plan to release the jQuery verification plug-in as part of the default Project template, and add support for automatic enforcement of DataAnnotation verification rules in client JavaScript. This allows developers to easily add verification rules on a Model or ViewModel object, regardless of where the object is used in the application, these rules are enforced on both the client and server.

If you do not want to Directly mark your model or view model class, you can also create a "buddy class" that is accompanied by your model class, and encapsulate DataAnnotaton rules. This function is also useful in such scenarios: VS directly generates/updates the attribute code of the class, and you cannot simply describe the features of the generated code (for example, classes generated by the LINQ to SQL or LINQ to Entities designer ).

In addition to the built-in support for DataAnnotations, ASP. net mvc V2 DefaultModelBinder class now has a new virtual method that can be overwritten in the subclass to easily integrate other verification frameworks (such as Castle Validator and EntLib Validation ). In ASP. net mvc, the verification UI auxiliary method is designed to support any type of verification frameworks (they do not need to know DataAnnotations ).

Auxiliary Method for Strongly typed UI

ASP. net mvc V2 contains new html ui helper methods that allow you to use a strong lambda expression when referencing a model object of a view template. This can facilitate better view compilation check (such defects are found during compilation rather than during runtime), and facilitate better support for code intelliisense in view templates.

You can see an example below to demonstrate better intelliisense. Note that when I use the new Html. EditorFor () helper method, how can I get a complete list of attributes of the Customer model object:

The first preview provides built-in support for the new Html. EditorFor (), Html. LabelFor (), and Html. DisplayFor () auxiliary methods. The updated MVC ures assembly that will be released this week will also contain other Html. textBoxFor (), Html. textAreaFor (), Html. dropDownListFor (), Html. hiddenFor (), and Html. supported ValidationMessageFor () auxiliary methods (over time, these methods will also be moved to the core ASP. net mvc 2 ).

Below, you can see an updated version of the "Create" view template in the Customer creation scenario. Note: In the UI auxiliary method, we use a strong lambda expression instead of a string expression to reference the Customer object. We can obtain the complete intelliisense and compile-time check in all these methods:

The preceding Html. LabelFor () method generates the <label for = "Name"> Name: </label> HTML identifier.

The Html. EditorFor () auxiliary method can be used for any data type value. By default, it is smart and outputs the appropriate HTML <input/> element based on the type to be edited. For example, the <input type = "text"/> element is generated for the first four attributes (string and integer, the <input type = "checkbox"/> element is generated for the last "IsActive" attribute because this attribute is of the boolean type.

In addition to simple data types, the Html. EditorFor () helper method also allows you to pass to IT complex objects with multiple attributes. By default, It loops all the public attributes of an object and outputs <label>, <input/> elements, and any suitable verification message for each attribute it can find. For example, we can rewrite the above view to make a single Html. EditorFor () call to the Customer object, and output the same identifier as above in terms of concept:

The strongly typed helper method allows you to use the [DisplayName] feature on the attributes of the Customer view class to control the tag string output for each attribute (for example: instead of using "IsActive" as the label text, we can add a [DisplayName ("Is Active Customer:")] feature ).

You can also add a [ScaffoldColumn (false)] feature to indicate that a complex object is transmitted to Html on the image. in scenarios such as EditorFor (), a specific attribute should not be displayed at all.

Template support for UI auxiliary methods

The Html. EditorFor () and Html. DisplayFor () auxiliary methods provide built-in support for displaying standard data types and complex objects containing multiple attributes. As mentioned above, they also support basic customization mechanisms such as the [DisplayName] and [ScaffoldColumn] features on the view model.

However, developers often want to be able to further customize the output of the UI auxiliary method and have full control over the generated items. The Html. EditorFor () and Html. DisplayFor () auxiliary methods support this requirement through a templated mechanism, which allows you to define external templates, replace the original ones, and completely control the display output. Even better, you can customize the content to be displayed based on each data type/class.

In the first preview, you can go to the \ Views \ [controller name] Directory (if you want to customize the display of Views used by a specific controller) or add an "EditorTemplates" or "DisplayTemplates" subdirectory to the \ Views \ Shared directory (if you want to customize the display of all Views and controllers in an application.

Then you can add the partial template file to these directories to customize the display output for individual data types or classes. For example, I add an EditorTemplates subdirectory under the \ Views \ Shared directory and add three custom template files to it:

The above "Customer. the ascx template indicates that I want to customize the template when calling Html. output when the parameter of EditorFor () is the Customer object (for example, I can customize the exact order and layout of the Customer attribute ). The above "DateTime. the ascx template indicates that I want to customize the template when calling Html. output when the parameter of EditorFor () is DateTime attribute (for example, I may want to use the calendar control of JavaScript, rather than a common text box ). You can also add an "Object. ascx" template in the directory. If you want to replace the default display of all objects.

In addition to customizing the output based on each class, you can also add "named templates" to the directory )". A common scenario may be the "CountryDropDown" template, which processes string data types, but does not provide standard text boxes, the <select> drop-down box that lists the country name values that a user can select. The following is an example of this editor template:

Then, we can pass the name of the preceding template as a parameter when calling the Html. EditorFor () helper method, explicitly indicating that we want to use this template. For example, in addition to specifying the lambda expression of the Country attribute, we also specify the name of the editor template to be used during display:

Alternatively, you can specify the "UIHint" attribute and type in your ViewModel. This allows you to specify the default editor or display template to be used in a single place, then use the specified template in all views of the application (without explicitly passing this name as a parameter to Html. editorFor ).

The following is an example of how to use the UIHint feature to indicate that the Customer. Country attribute (string type) should be displayed by default using the CountryDropDown template:

Once the preceding features are set in our view model, when you use Html. EditorFor () to display the attributes, you no longer need to explicitly specify the Template Name. Now, when you click Refresh on/Mers MERs/Create URL, our Country attribute will be displayed as a drop-down box instead of a standard text box:

 Other cool features

The first preview of ASP. net mvc 2 also contains several small but wonderful features. My favorites include:

New [HttpPost] features

In ASP. net mvc, It is very common to divide the processing of a URL into two action methods, one of which processes GET requests and the other processes POST requests.

In ASP. net mvc 1, you use the [AcceptVerbs (HttpVerbs. Post)] feature to indicate the "Post" version of the action method:

This still works in ASP. net mvc 2, but now you can use the more concise [HttpPost] feature to do the same thing:

 Default parameter value

It is common to process customizable parameters in web scenarios. In ASP. in. net mvc 1, there are generally two methods for processing province parameters. By registering a custom path rule, you can specify the default value in it, or mark the parameter of an action method as nullable, then add the code in the action method to process whether the parameter is null (if it is null, the default value is provided ).

ASP. net mvc 2's first preview now supports the DefaultValueAttribute in the System. ComponentModel namespace on the parameters of the action method. This allows you to specify the parameter value that ASP. net mvc should pass to the action method when a parameter is not in the request value. For example, how can we handle/Products/Browse/Beverages and/Products/Browse/Beverages? In the example of page = 2 URLs, if the "page" parameter is not part of the query string, its value is "1 ":

Today, VB allows you to specify the default parameter values directly in the language (instead of explicitly specifying the DefaultValue feature as above). C # language in VS2010 also supports the default value of the parameter that can be saved, we can simplify the above Code:

This should make the processing of default/saving scenarios very clean.

 Bind binary data

In the first preview of ASP. net mvc 2, a base64 encoded string value is added to the attributes of the byte [] and System. Data. Linq. Binary types. There are now two Html. Den den () versions that can accept these data types. This is useful when you want to enable concurrency control in the application and transfer the timestamp value of the database row record back and forth in the form.

 Conclusion

Click here to download an ASP. net mvc 2 destination .zip file. This project implements the example I demonstrated above.

Today's ASP. net mvc 2 is only the first preview version, and more features will be available in the future. The development team is looking forward to receiving a lot of feedback on how to improve and enhance functions.

The purpose of regularly releasing these previews is to help ensure that the feedback process is open and that anyone who wants to participate can easily participate. Post feedback, suggestions, or problems on the ASP. net mvc forum at www.asp.net. You can also learn more about this preview version from Phil Haack's MVC2 post and Phil and Scott Hanselman's recording of the first preview version in Channel9.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.