MVC Validation

Server-side validation

Server-side validation should be done whether we validate on the client or not. Users could disable JavaScript or do something unexpected to bypass client-side validation, and the server is the last line of defense protecting our data from dirty input. Some
validation rules require server-side processing—network topology might require that only the server has access to some external resource required to validate input.

  • Validation with Data Annotations

Data Annotations, introduced with the .NET 3.5 SP1 release, are a set of attributes and classes defined in the System.ComponentModel.DataAnnotations assembly that allow you to decorate your classes with metadata. This metadata describes a set of rules that can be used to determine how a particular object should be validated.

The Data Annotations attributes used for validation

AttributeDescription
CompareAttribute

Compares the value of two model properties—if they are equal,
validation succeeds

RemoteAttribute

Instructs jQuery Validate, the default client validation library, to
call an action on the server to perform server-side validation
with a client-side experience

RequiredAttributeSpecifies that a data field value is required
RangeAttributeSpecifies the numeric range constraints for the value of a data field
RegularExpressionAttribute

Specifies that a data field value must match the specified
regular expression

StringLengthAttribute

Specifies the maximum number of characters that are allowed
in a data field

 

  • Display potential validation error messages
    • If we’re using the model templates, validation messages are already included in the template.The default editor model templates generate a user interface that includes side by side input elements and validation messages.
      <h2>Edit</h2>
      @using (Html.BeginForm("Edit", "Home"))
      {
           @Html.EditorForModel()
           <button type="submit">Submit</button>
      }
    • ValidationSummary & ValidationMessage - The ValidationSummary extension provides a summary list of validation errors, usually displayed at the top of the form. For validation errors for specific model properties, we can use the ValidationMessage and expression-based ValidationMessageFor methods.

 

Client-side validation

  • Import URLs of scripts - Because each JavaScript library builds on others, it’s important that the files be included in the correct order.
<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

 

  • Enable Client-side validation in Web.config
 <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>
  • RemoteAttribute

A new validation attribute in ASP.NET MVC 3 is RemoteAttribute. Decorating a model property with this attribute will instruct jQuery Validate to make an HTTP request to a given action method for server-side checking. The result is transmitted back to the client, and an error message will be displayed before the form is submitted.

转载于:https://www.cnblogs.com/fast-michael/archive/2012/08/20/2647878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值