mvc中动态给一个Model类的属性设置验证
2014-06-28 13:44
357 查看
原文:
mvc中动态给一个Model类的属性设置验证
在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这样的验证毕竟功能有限,那么就需要我们自己进行定制验证。
假设有Model类:class Dinners{
private string Title;
private System.DateTime EventDate;
private string Description;
private string HostedBy;
private string ContactPhone;
private string Address;
private string Country;
private double Latitude;
private double Longitude;
}
这里需要对该类的每个属性都加上不可为空的约束,并对电话号码、日期做验证。代码如下:
public partial class Dinners
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Title))
{
yield return new RuleViolation("Title required", "Title");
}
if (String.IsNullOrEmpty(Description))
{
yield return new RuleViolation("Description required", "Description");
}
if (String.IsNullOrEmpty(HostedBy))
{
yield return new RuleViolation("HostedBy required", "HostedBy");
}
if (String.IsNullOrEmpty(Address))
{
yield return new RuleViolation("Address required", "Address");
}
if (String.IsNullOrEmpty(Country))
{
yield return new RuleViolation("Country required", "Country");
}
if (String.IsNullOrEmpty(ContactPhone))
{
yield return new RuleViolation("Phone# required", "ContactPhone");
}
//这里引用了类PhoneValidator(自定义验证类,不再详述)
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
{
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
}
yield break;
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
{
throw new ApplicationException("Rule violations prevent saving");
}
}
}
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
前台验证代码:
<h2>Edit Dinner</h2>
<%=Html.ValidationSummary("Please crrect the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<p>
<label for="Title">Dinner Title:</label>
<%=Html.TextBox("Title") %>
<%=Html.ValidationMessage("Title","*") %>
</p>
<p>
<label for="EventDate">EventDate:</label>
<%=Html.TextBox("EventDate",string.Format("{0:g}",Model.EventDate)) %>
<%=Html.ValidationMessage("EventDate","*") %>
</p>
<p>
<label for="Description">Description:</label>
<%=Html.TextArea("Description") %>
<%=Html.ValidationMessage("Description","*") %>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.TextArea("Address") %>
<%=Html.ValidationMessage("Address","*") %>
</p>
<p>
<label for="Country">Country:</label>
<%=Html.TextBox("Country") %>
<%=Html.ValidationMessage("Country","*") %>
</p>
<p>
<label for="ContactPhone">ContactPhone #:</label>
<%=Html.TextBox("ContactPhone") %>
<%=Html.ValidationMessage("ContactPhone","*") %>
</p>
<p>
<label for="Latitude">Latitude:</label>
<%=Html.TextBox("Latitude") %>
<%=Html.ValidationMessage("Latitude","*") %>
</p>
<p>
<label for="Longitude">Longitude:</label>
<%=Html.TextBox("Longitude") %>
<%=Html.ValidationMessage("Longitude","*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
注:代码来自教程《一步一步学习asp.net mvc》
mvc中动态给一个Model类的属性设置验证
在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这样的验证毕竟功能有限,那么就需要我们自己进行定制验证。
假设有Model类:class Dinners{
private string Title;
private System.DateTime EventDate;
private string Description;
private string HostedBy;
private string ContactPhone;
private string Address;
private string Country;
private double Latitude;
private double Longitude;
}
这里需要对该类的每个属性都加上不可为空的约束,并对电话号码、日期做验证。代码如下:
public partial class Dinners
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Title))
{
yield return new RuleViolation("Title required", "Title");
}
if (String.IsNullOrEmpty(Description))
{
yield return new RuleViolation("Description required", "Description");
}
if (String.IsNullOrEmpty(HostedBy))
{
yield return new RuleViolation("HostedBy required", "HostedBy");
}
if (String.IsNullOrEmpty(Address))
{
yield return new RuleViolation("Address required", "Address");
}
if (String.IsNullOrEmpty(Country))
{
yield return new RuleViolation("Country required", "Country");
}
if (String.IsNullOrEmpty(ContactPhone))
{
yield return new RuleViolation("Phone# required", "ContactPhone");
}
//这里引用了类PhoneValidator(自定义验证类,不再详述)
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
{
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
}
yield break;
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
{
throw new ApplicationException("Rule violations prevent saving");
}
}
}
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
前台验证代码:
<h2>Edit Dinner</h2>
<%=Html.ValidationSummary("Please crrect the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<p>
<label for="Title">Dinner Title:</label>
<%=Html.TextBox("Title") %>
<%=Html.ValidationMessage("Title","*") %>
</p>
<p>
<label for="EventDate">EventDate:</label>
<%=Html.TextBox("EventDate",string.Format("{0:g}",Model.EventDate)) %>
<%=Html.ValidationMessage("EventDate","*") %>
</p>
<p>
<label for="Description">Description:</label>
<%=Html.TextArea("Description") %>
<%=Html.ValidationMessage("Description","*") %>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.TextArea("Address") %>
<%=Html.ValidationMessage("Address","*") %>
</p>
<p>
<label for="Country">Country:</label>
<%=Html.TextBox("Country") %>
<%=Html.ValidationMessage("Country","*") %>
</p>
<p>
<label for="ContactPhone">ContactPhone #:</label>
<%=Html.TextBox("ContactPhone") %>
<%=Html.ValidationMessage("ContactPhone","*") %>
</p>
<p>
<label for="Latitude">Latitude:</label>
<%=Html.TextBox("Latitude") %>
<%=Html.ValidationMessage("Latitude","*") %>
</p>
<p>
<label for="Longitude">Longitude:</label>
<%=Html.TextBox("Longitude") %>
<%=Html.ValidationMessage("Longitude","*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
注:代码来自教程《一步一步学习asp.net mvc》
相关文章推荐
- mvc中动态给一个Model类的属性设置验证
- ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则
- ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则
- 如何根据一个规则表动态地在Form的初始化时设置控件的属性
- 在Asp.Net MVC中实现RequiredIf标签对Model中的属性进行验证
- MVC-Model数据注解(三)-Remote验证的一个注意事项
- MVC 验证和异常处理 分开处理Model级别和属性级别验证
- ASP.NET MVC 学习7、为Model Class的字段添加验证属性(validation attribuate)
- MVC-Model数据注解(三)-Remote验证的一个注意事项
- MVC-Model数据注解(三)-Remote验证的一个注意事项
- AngularJS中巧用ngModel的$asyncValidators属性写一个验证唯一性的Direcitve
- MVC中获取模型属性的Range和StringLength验证特性设置
- 在Asp.Net MVC中实现CompareValues标签对Model中的属性进行验证
- QTP如何设置动态属性的标准检查点? 此业务是:点击【生成验证码】按钮,会在一个文本框中生成验证码
- 客户端动态设置DropShadow控件时,$find()与BehaviorID属性的用途
- 地磅称量系统之(24~26)创建一个名称为WinApp的Windows应用程序作为启动项目并且设置主界面上控件的属性
- 一个能设置验证信息样式的WebTextBox基本验证复合控件
- ASP.NET MVC+LINQ开发一个图书销售站点(8):表单验证
- JSP培训(7)——使用JDBC完成动态验证、采用MVC完成数据查询
- javascript动态生成表格内容并设置属性,获取选中行的值