What's the difference between MVC1,MVC2,MVC3?

Source: Internet
Author: User

MVC1 mode:

View receives user input and uploads commands to controller
Controller processing command, update model
When model is updated, it notifies the view that update is required
View updates to the user

MVC2 mode:

As MVC1, model can notify view, and then view can be updated, which is common in Windows programs, such as MFC's Frame-document-view architecture, if the document changes, he will actively notify view for update

However, in the Web, when the Java bean for model is updated, the JSP that is the view cannot be invoked (and view is usually a lot and needs to be selected), and therefore changed to MVC2 mode:

View receives user input and uploads commands to controller
Controller processing command, update model
Model is updated, controller will choose a view and forward to this JSP, with the model put to the request parameter
This view gets model and then displays asp.net MVC 3 new features full resolution

ASP.net MVC3, based on ASP.net MVC 1 and 2, adds a lot of features that make code simpler and can be extended in depth. This article provides a description of many of the new features included in this release.

Razor View Engine
ASP.net MVC3 brings a new view engine named Razor, which provides the following benefits:

Razor now offers a number of new features:

Razor also contains new HTML Helper, for example:

Support for multiple view engines
In ASP.net MVC3, the Add View dialog allows you to select the view engine you want, and in the new Project dialog you can specify the default view engine for the project, select Webform,razor, or open source view engine, such as: Spark, Nhaml, or Ndjango.

Improvement of the Controller
Global Action Filter
Sometimes you want to be able to perform some processing logic before or after an Action method executes, and in ASP.net MVC2, you provide an action filter that allows you to handle the action method for a particular controller, and in fact, you want to do all of the action For similar processing, MVC3 allows you to add filters to the Globalfilters collection to create a global filter.

The new ViewBag property
The controller in MVC2 supports the ViewData property, allowing the data to be passed to the view template through a later-bound dictionary, which you can do more simply by ViewBag in MVC3. For example, for viewdata["message" = "text", you can do it by Viewbag.message = "text." You do not need to define any strongly typed properties by class, because this is a dynamic property, and internally, the ViewBag property is stored in the ViewData dictionary as a name-value pair. Note that in many pre-release versions, this property is referred to as ViewModel.

New type of ActionResult
The following actionresult types are added or expanded in MVC3.

JavaScript and Ajax Improvements
By default, in MVC3, Ajax and validation use an unobtrusive unobtrusive JavaScript approach. Unobtrusive does not insert inline JavaScript in HTML, which makes HTML more streamlined and less intrusive, and makes it easier to replace and customize the JavaScript library, in MVC3, the validation Assistant uses the Jquery.validate plugin by default Done, if you want to use MVC2 behavior, you can web.config the configuration to close the unobtrusive.

Client-side validation is enabled by default
In earlier versions of MVC, you need to explicitly call the Html.enableclientvalidation method in the view to enable client-side validation. In MVC3, it is no longer needed because client-side validation is enabled by default. Can be closed in web.config.

To make the client-side validation work, you still need to include a reference to the JQuery and jquery.validation libraries on your site, either on your website or on a CDN server from Microsoft or Google.

Remote Authentication
ASP.net 3 provides support for remote authentication of the JQuery Validation plug-in through a new label Remoteattribute. This allows the client's validation library to automatically invoke a custom method that you define on the server to complete the validation logic that can only be done on the server.

In the following example, the Remote tag specifies that the user name field is validated by a method named Usernameavailable defined in Userscontroller. Razor's syntax is simple and clear, only need to minimize the input Razor easy to learn, syntax similar to C # and VB Visual Studio provides smart hints and syntax coloring for Razor Razor views do not need to allow programs or start a WEB server to test @ Model is used to specify the model type to upload to the view @* * Annotation syntax you can set default items, such as layouts, for the entire site at once. The Html.raw method provides output that does not have HTML encoding to support the sharing of code between multiple views (_viewstart.cshtml or _viewstart.vbhtml) Chart. Generate chart WebGrid, generate data tables, support full paging and sorting Crypto, use hash algorithm to create hash and add salt password webimage, generate picture WebMail, send email httpnotfoundresult. Returns the 404 HTTP status to the client. Redirectresult. Based on a Boolean parameter that returns a temporary redirect (HTTP 302 status code) or persistent redirection (HTTP the Status code), Controller provides three ways to support persistent redirection: Redire Ctpermanent, Redirecttoroutepermanent, and Redirecttoactionpermanent. These methods return a Redirectresult object instance where the permanent property is true. Httpstatuscoderesult. Returns the HTTP status code specified by the user. public class user     {     [Remote ("UserNameAvailable",  "Users")]     public string username { get; set; }    }   

The following code defines the public class Userscontroller {public bool Usernameavailable (string username) {if (Myreposito) in the controller Ry.     Usernameexists (username)) {return "false";     Return "true"; }     }

For more resources on remote properties, refer to how to:implement Remote Validation in asp.net MVC http://msdn.microsoft.com/en-us/library/gg508808 ( vs.98). aspx

JSON Binding Support
The ASP.net MVC3 contains built-in JSON binding support, allowing the action method to receive JSON-encoded data and model the action parameters. This capability is often used in client-side templates and data binding. Client templates allow you to format and display one or more data through a client template, MVC3 allows you to simply connect client templates and server-side Action methods, send and receive data via JSON, and more information reference: Scott Guthrie's MVC 3 Preview blog post.

Validationattribute class
The improved Validationattribute class in. NET FRAMEWORK4 supports the new IsValid overload, providing more information about the current validation context, such as what object was validated. This allows you to validate the current value based on other properties of model, for example, the new compareattribute allows you to compare the values of the model's two properties, in the following example, the ComparePassword attribute must match the Password field with the validation.

Improvement of Model Verification
DataAnnotations Meta data label
asp.net MVC3 supports dataannotations metadata labels, such as: DisplayAttribute.     public class User {[Required] public string Password {get; set;}     [Required, Compare ("Password")] public string ComparePassword {get; set;} }

Validating Interfaces

The Ivalidatableobject interface allows you to perform model-level validation and allows you to provide validation error messages for the entire mode state, or two properties based on models. When the model is bound, MVC3 receives error messages from Ivalidatableobject, and the affected fields are automatically identified or highlighted when the built-in HTML Assistant is used in the view.

The Iclientvalidatable interface allows ASP.net MVC to discover supported client validators at runtime, which is used to support the integration of different validation frameworks.

More about verifying the interface content, refer to Scott Guthrie's MVC 3 Preview blog post in Model Validation improvements section.

Improvement of dependency injection for dependency Injection
asp.net MVC3 provides better di and IoC support, and supports DI in the following places:

MVC3 supports the Common Service Locator Library and any DI containers that support the Iservicelocator interface for this library. New Idependencyresolver interfaces that are easy to integrate into the DI framework are also supported.

For more information on DI, refer to:

Other new features

NuGet Integration
asp.net MVC3 automatically installs and enables NuGet, NuGet is a free open source Package Manager that makes it easy to find, install, and use. NET libraries in your project. It can work with all of the Visual Studio project types, including ASP.net webform and MVC.

NuGet allows developers to maintain open source projects, such as MOQ projects, NHibernate, and so on, to register them in an online Web site.

More information reference: NuGet documentation on the CodePlex site.

Output caching for partial pages
asp.net MVC supports full-page caching starting with version 1, and MVC3 also provides partial-page caching. This allows you to easily cache an area or fragment of output, with more content referenced by Scott Guthrie's blog post on the MVC 3 Release Candidate Partial Page output Caching paragraph, or There are MVC 3 release Notes in the child Action Output Caching paragraph.

Granularity control in Request verification
asp.net MVC has built a request validation mechanism to automatically help handle similar cross-site attacks and HTML injections, among other things. In fact, sometimes you want to be able to explicitly turn off verification of requests, such as if you want to allow users to submit HTML content, for example, in a content management system, now you can support the ability to turn off request validation based on a property when binding, by adding the allowhtml tag to model or view model. More information to refer to:

Extensible New Project dialog box

In MVC3, you can add a project template, a view engine, a unit test project framework to the New Project dialog box.

Improvement of Scaffolding
Scaffolding in MVC3 provides better support for primary keys, for example, scaffolding templates do not add primary keys to the edit form.

By default, the scaffolding created and edited now replaces the Html.textboxfor assistant with the Html.editorfor Helper, which supports the metadata label in the model when the View dialog box is added to produce a view.

New overloads for Html.labelfor and Html.labelformodel PDFs
New method overloads are added for Labelfor and Labelformodel, allowing you to specify or override the Label text.

Non-session controller support
In MVC3, you can specify whether the controller uses session state and, in turn, whether the session is read-write or read-only.

The new Additionalmetadataattributepdf class
You can access the Modelmetadata.additionalvalues dictionary by using the Additionalmetadataattribute tag on one of the properties of model, for example, if a property of the models only supports administrator display, you can set the following: Controller (registering and injecting controller factories, injecting controllers). View (registering and injecting view engines, injecting dependencies into view pages). Action filter (Locating and injecting filters). Model binding device (registering and injecting). Model validation provider (registering and injecting). Model metadata provider (registering and injecting). The Value provider buys and sells IC networks (registering and injecting). Brad Wilson ' s series of blog posts on Service Location MVC 3 release Notes Scott Guthrie's blog post on the MVC 3 release The unobtrusive JavaScript and Validation section in candidate. MVC 3 Release Notes

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.