当前位置:首页 > 开发教程 >

ASP.NET MVC 3(2)

时间:2013-04-26 15:47 来源:网络整理 作者:采集侠 收藏

In earlier versions of MVC, you need to explicitly call the Html.EnableClientValidation method from a view in order to enable client-side validation. In MVC 3 this is no longer required because clien

In earlier versions of MVC, you need to explicitly call the Html.EnableClientValidation method from a view in order to enable client-side validation. In MVC 3 this is no longer required because client-side validation is enabled by default. (You can disable this using a setting in the web.config file.)

In order for client-side validation to work, you still need to reference the appropriate jQuery and jQuery Validation libraries in your site. You can host those libraries on your own server or reference them from a content delivery network (CDN) like the CDNs from Microsoft or Google.

Remote Validator

ASP.NET MVC 3 supports the new RemoteAttribute class that enables you to take advantage of the jQuery Validation plug-in's remote validator support. This enables the client-side validation library to automatically call a custom method that you define on the server in order to perform validation logic that can only be done server-side.

In the following example, the Remote attribute specifies that client validation will call an action named UserNameAvailable on the UsersController class in order to validate the UserName field.

public class User { [Remote("UserNameAvailable", "Users")] public string UserName { get; set; } }

The following example shows the corresponding controller.

public class UsersController { public bool UserNameAvailable(string username) { if(MyRepository.UserNameExists(username)) { return "false"; } return "true"; } }

For more information about how to use the Remote attribute, see How to: Implement Remote Validation in ASP.NET MVC in the MSDN library.

JSON Binding Support

ASP.NET MVC 3 includes built-in JSON binding support that enables action methods to receive JSON-encoded data and model-bind it to action-method parameters. This capability is useful in scenarios involving client templates and data binding. (Client templates enable you to format and display a single data item or set of data items by using templates that execute on the client.) MVC 3 enables you to easily connect client templates with action methods on the server that send and receive JSON data. For more information about JSON binding support, see the JavaScript and AJAX Improvements section of Scott Guthrie's MVC 3 Preview blog post.

Model Validation Improvements "DataAnnotations" Metadata Attributes

ASP.NET MVC 3 supports DataAnnotations metadata attributes such as DisplayAttribute.

"ValidationAttribute" Class

The ValidationAttribute class was improved in the .NET Framework 4 to support a new IsValid overload that provides more information about the current validation context, such as what object is being validated. This enables richer scenarios where you can validate the current value based on another property of the model. For example, the new CompareAttribute attribute lets you compare the values of two properties of a model. In the following example, the ComparePassword property must match the Password field in order to be valid.

public class User { [Required] public string Password { get; set; } [Required, Compare("Password")] public string ComparePassword { get; set; } } Validation Interfaces

The IValidatableObject interface enables you to perform model-level validation, and it enables you to provide validation error messages that are specific to the state of the overall model, or between two properties within the model. MVC 3 now retrieves errors from the IValidatableObject interface when model binding, and automatically flags or highlights affected fields within a view using the built-in HTML form helpers.

The IClientValidatable interface enables ASP.NET MVC to discover at run time whether a validator has support for client validation. This interface has been designed so that it can be integrated with a variety of validation frameworks.

For more information about validation interfaces, see the Model Validation Improvements section of Scott Guthrie's MVC 3 Preview blog post. (However, note that the reference to "IValidateObject" in the blog should be "IValidatableObject".)

Dependency Injection Improvements

ASP.NET MVC 3 provides better support for applying Dependency Injection (DI) and for integrating with Dependency Injection or Inversion of Control (IOC) containers. Support for DI has been added in the following areas:

MVC 3 supports the Common Service Locator library and any DI container that supports that library's IServiceLocator interface. It also supports a new IDependencyResolver interface that makes it easier to integrate DI frameworks.

For more information about DI in MVC 3, see the following resources:

  • Brad Wilson's series of blog posts on Service Location
  • MVC 3 Release Notes
  • Other New Features NuGet Integration

    开发教程阅读排行

    最新文章