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》
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。