Asp.net mvc ASP.NET MVC2应用程序的自定义验证规则
我正在尝试向我的应用程序添加验证。在允许将信息写入数据库之前,我需要检查一些规则。我已将基本数据验证添加到模型中,但我还需要确保,如果一个字段具有特定值,则另一个字段是必需的。曾有一段时间,美国大学的NerdDinner教程介绍了这一点,我过去用它进行验证,但现在我找不到这一点或任何其他例子。这是我的模型:Asp.net mvc ASP.NET MVC2应用程序的自定义验证规则,asp.net-mvc,asp.net-mvc-2,model-validation,Asp.net Mvc,Asp.net Mvc 2,Model Validation,我正在尝试向我的应用程序添加验证。在允许将信息写入数据库之前,我需要检查一些规则。我已将基本数据验证添加到模型中,但我还需要确保,如果一个字段具有特定值,则另一个字段是必需的。曾有一段时间,美国大学的NerdDinner教程介绍了这一点,我过去用它进行验证,但现在我找不到这一点或任何其他例子。这是我的模型: public class DayRequested { public int RequestId { set; get; } [Required, DisplayName("
public class DayRequested
{
public int RequestId { set; get; }
[Required, DisplayName("Date of Leave")]
public string DateOfLeave { get; set; }
[Required, DisplayName("Time of Leave")]
public string TimeOfLeave { get; set; }
[Required, DisplayName("Hours Requested")]
[Range(0.5, 24, ErrorMessage = "Requested Hours must be within 1 day")]
public double HoursRequested { get; set; }
[Required, DisplayName("Request Type")]
public string RequestType { get; set; }
[DisplayName("Specify Relationship")]
public string Relationship { get; set; }
[DisplayName("Nature of Illness")]
public string NatureOfIllness { get; set; }
public bool AddedToTimesheet { get; set; }
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(DateOfLeave))
yield return new RuleViolation("Date of Leave Required", "DateOfLeave");
if (String.IsNullOrEmpty(TimeOfLeave))
yield return new RuleViolation("Date of Leave Required", "TimeOfLeave");
if ((HoursRequested < 0.5) || (HoursRequested > 24))
yield return new RuleViolation("Hours must be in a period of one day", "HoursRequested");
if (String.IsNullOrEmpty(RequestType))
yield return new RuleViolation("Request Type is required", "RequestType");
if ((!String.IsNullOrEmpty(NatureOfIllness)) && (NatureOfIllness.Length < 3))
yield return new RuleViolation("Nature of Illness must be longer 2 characters", "NatureOfIllness");
// Advanced data validation to make sure rules are followed
LeaveRequestRepository lrr = new LeaveRequestRepository();
List<LeaveRequestType> lrt = lrr.GetAllLeaveRequestTypes();
LeaveRequestType workingType = lrt.Find(b => b.Id == Convert.ToInt32(RequestType));
if ((String.IsNullOrEmpty(Relationship)) && (workingType.HasRelationship))
yield return new RuleViolation("Relationship is Required", "Relationship");
if ((String.IsNullOrEmpty(NatureOfIllness)) && (workingType.HasNatureOfIllness))
yield return new RuleViolation("Nature of Illness is Required", "NatureOfIllness");
yield break;
}
}
RequestEditor.ascx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create New Leave Request</h2>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
<%= Html.Partial("RequestEditor", Model) %>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
</asp:Content>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<table id="editorRows">
<% foreach (var item in Model.DaysRequested)
Html.RenderPartial("RequestedDayRow", new EmployeePayroll.ViewModels.LeaveRequestRow(item, Model.LeaveRequestType)); %>
</table>
<p>Type your time to sign your request.</p>
<p><%= Html.LabelFor(model => model.LeaveRequest.EmployeeSignature) %>:
<%= Html.TextBoxFor(model => model.LeaveRequest.EmployeeSignature, new { Class="required" })%>
<%= Html.ValidationMessageFor(model => model.LeaveRequest.EmployeeSignature)%></p>
<p><input type="submit" value="Submit Request" /></p>
<% } %>
<tbody class="editorRow">
<tr class="row1"></tr>
<tr class="row2">
<td colspan="2" class="relationship">
<%= Html.LabelFor(model => model.DayRequested.Relationship)%>:
<%= Html.TextBoxFor(model => model.DayRequested.Relationship) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.Relationship)%>
</td>
<td colspan="2" class="natureOfIllness">
<%= Html.LabelFor(model => model.DayRequested.NatureOfIllness)%>:
<%= Html.TextBoxFor(model => model.DayRequested.NatureOfIllness) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.NatureOfIllness)%>
</td>
<td></td>
</tr>
</tbody>
键入签署请求的时间
model.LeaveRequest.EmployeeSignature)%>:
model.LeaveRequest.EmployeeSignature,新的{Class=“required”})%>
model.LeaveRequest.EmployeeSignature)%>
RequestedDayRow.ascx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create New Leave Request</h2>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
<%= Html.Partial("RequestEditor", Model) %>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
</asp:Content>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<table id="editorRows">
<% foreach (var item in Model.DaysRequested)
Html.RenderPartial("RequestedDayRow", new EmployeePayroll.ViewModels.LeaveRequestRow(item, Model.LeaveRequestType)); %>
</table>
<p>Type your time to sign your request.</p>
<p><%= Html.LabelFor(model => model.LeaveRequest.EmployeeSignature) %>:
<%= Html.TextBoxFor(model => model.LeaveRequest.EmployeeSignature, new { Class="required" })%>
<%= Html.ValidationMessageFor(model => model.LeaveRequest.EmployeeSignature)%></p>
<p><input type="submit" value="Submit Request" /></p>
<% } %>
<tbody class="editorRow">
<tr class="row1"></tr>
<tr class="row2">
<td colspan="2" class="relationship">
<%= Html.LabelFor(model => model.DayRequested.Relationship)%>:
<%= Html.TextBoxFor(model => model.DayRequested.Relationship) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.Relationship)%>
</td>
<td colspan="2" class="natureOfIllness">
<%= Html.LabelFor(model => model.DayRequested.NatureOfIllness)%>:
<%= Html.TextBoxFor(model => model.DayRequested.NatureOfIllness) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.NatureOfIllness)%>
</td>
<td></td>
</tr>
</tbody>
model.DayRequested.Relationship)%>:
model.DayRequested.Relationship)%>
model.DayRequested.Relationship)%>
型号.DayRequested.NatureOfIllness)%>:
型号.DayRequested.NatureOfIllness)%>
型号.DayRequested.NatureOfIllness)%>
非常简单-只需将验证属性应用于整个模型(或子类)。然后,validation属性将获得对模型的引用,而不仅仅是一个属性,并且您可以对多个属性执行检查 您应该查看密码验证以了解如何执行此操作的示例
请在此处查看PropertiesMustMatch验证程序:
是的,我在发布这篇文章后再次回顾。NerdDinner示例使用了一个名为
ModelState.AddRuleViolations()
的助手方法。这将违规行为添加到了ModelState.addmodeleror()
谢谢!但这不是他想要的。它显示了使用验证属性比较两个属性。