Part 84 - Compare attribute in asp.net mvc
来源:互联网 发布:郑州做软件开发 编辑:程序博客网 时间:2024/05/05 03:10
Compare attribute is used to compare 2 properties of a model. Comparing email addresses and passwords is the common use case of Compare attribute. Let's understand using Compare attribute with an example. We will be continuing with the example, that we discussed in Part 83.
To ensure that the user has typed the correct email, let's include "Confirm Email" field on the "Edit" view. To achieve this, add "ConfirmEmail" property in "Employee" class in Employee.cs class file that is present in "Models" folder.
public partial class Employee
{
public string ConfirmEmail { get; set; }
}
At this point you may get this question. Why are we not adding this property to EmployeeMetaData class. This is because EmployeeMetaData class, is used to associate metadata for the properties that are already present in the auto-generated Employee class. The auto-generated Employee class is present in SampleDataModel.Designer.cs file in Models folder. ConfirmEmail property does not exist in auto-generated Employee class. It is a new property that we want to add to Employee model class. ConfirmEmail property is going to be used only for validation. We will not be saving the value of this property to the database table. We will be stroing Emial property value to the database.
Build the solution, so that the Employee class is compiled.
Copy and paste the following 2 div tags, to add ConfirmEmail field to the Edit View.
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmEmail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmEmail)
@Html.ValidationMessageFor(model => model.ConfirmEmail)
</div>
Finally, decorate ConfirmEmail property in Employee class with Compare attribute. Most of the validation attributes are present in System.ComponentModel.DataAnnotations namespace, but Compare attribute is present in System.Web.Mvc namespace.
public partial class Employee
{
[Compare("Email")]
public string ConfirmEmail { get; set; }
}
At this point, this confirm attribute will ensure Email and ConfirmEmail properties have the same values. If they don't, then a validation error message is displayed.
- Part 84 - Compare attribute in asp.net mvc
- Part 81 - Range attribute in asp.net mvc
- Part 82 - Creating custom validation attribute in asp.net mvc
- Part 83 - RegularExpression attribute in asp.net mvc
- Part 60 - ViewStart in asp.net mvc
- Part 86 - ValidationSummary in asp.net mvc
- Authorize attribute and jquery AJAX in asp.net MVC
- Part 71 - childactiononly attribute in mvc
- Part 72 - HandleError attribute in mvc
- Part 2 - View engines in asp.net mvc
- Part 54 - T4 templates in asp.net mvc
- Part 85 - Enable client side validation in asp.net mvc
- Part 88 - Unobtrusive validation in asp.net mvc
- Part 89 - Remote validation in asp.net mvc
- ASP.NET MVC 4 Attribute特性
- ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 计算经纬度、距离、方位角
- QT Creator基础设置
- 394.Coins in a Line-硬币排成线(中等题)
- Part 83 - RegularExpression attribute in asp.net mvc
- android知识回顾-----单向链表
- Part 84 - Compare attribute in asp.net mvc
- android知识回顾-----开源框架-----Volley
- QT/C++ 下调用python自定义模块
- dom级别
- Part 85 - Enable client side validation in asp.net mvc
- java泛型
- HDU 1215 七夕节(筛选法)
- Part 86 - ValidationSummary in asp.net mvc
- 欢迎使用CSDN-markdown编辑器