我想验证我的视图中的任何2个文本框字段都提供了一个值。我做了这个型号验证:ASP.NET MVC 2问题与自定义ModelValidator
public class RequireEitherValidator : ModelValidator
{
private readonly string compareProperty;
private readonly string errorMessage;
public RequireEitherValidator(ModelMetadata metadata,
ControllerContext context, string compareProperty, string errorMessage)
: base(metadata, context)
{
this.compareProperty = compareProperty;
this.errorMessage = errorMessage;
}
public override IEnumerable<ModelValidationResult> Validate(object container)
{
if (Metadata.Model == null)
yield break;
var propertyInfo = container.GetType().GetProperty(compareProperty);
if (propertyInfo == null)
throw new InvalidOperationException("Unknown property:" + compareProperty);
string valueToCompare = propertyInfo.GetValue(container, null).ToString();
if (string.IsNullOrEmpty(Metadata.Model.ToString()) && string.IsNullOrEmpty(valueToCompare))
yield return new ModelValidationResult
{
Message = errorMessage
};
}
}
这验证逻辑从来没有被击中,我认为这是因为值被提供给文本框。
如果你需要它,这里的供应商和属性我与属性的使用创造了沿:
public class MyValidatorProvider : AssociatedValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(
ModelMetadata metadata, ControllerContext context,
IEnumerable<Attribute> attributes)
{
foreach (var attrib in attributes.OfType<RequireEitherAttribute>())
yield return new RequireEitherValidator(metadata, context,
attrib.CompareProperty, attrib.ErrorMessage);
}
}
public class RequireEitherAttribute : Attribute
{
public readonly string CompareProperty;
public string ErrorMessage { get; set; }
public RequireEitherAttribute(string compareProperty)
{
CompareProperty = compareProperty;
}
}
public class StudentLogin
{
[DisplayName("Last Name")]
[Required(ErrorMessage = "You must supply your last name.")]
public string LastName { get; set; }
[DisplayName("Student ID")]
[RegularExpression(@"^\d{1,8}$", ErrorMessage = "Invalid Student ID")]
[RequireEither("SSN", ErrorMessage = "You must supply your student id or social security number.")]
public int? StudentId { get; set; }
[DisplayName("Social Security Number")]
[RegularExpression(@"^\d{9}|\d{3}-\d{2}-\d{4}$", ErrorMessage = "Invalid Social Security Number")]
public string SSN { get; set; }
}
我的观点:接近这不仅仅是创建一个
<%Html.BeginForm(); %>
<p>
Please supply the following information to login:</p>
<ol class="standard">
<li>
<p>
<%=Html.LabelFor(x => x.LastName) %><br />
<%=Html.TextBoxFor(x => x.LastName)%>
<%=Html.ValidationMessageFor(x => x.LastName) %></p>
</li>
<li>
<p>
<%=Html.LabelFor(x => x.StudentId) %><br />
<%=Html.TextBoxFor(x => x.StudentId) %>
<%=Html.ValidationMessageFor(x => x.StudentId) %></p>
<p style="margin-left: 4em;">
- OR -</p>
<p>
<%=Html.LabelFor(x => x.SSN)%><br />
<%=Html.TextBoxFor(x => x.SSN) %>
<%=Html.ValidationMessageFor(x => x.SSN) %>
</p>
</li>
</ol>
<%=Html.SubmitButton("submit", "Login") %>
<%Html.EndForm(); %>
我喜欢它。我会尝试一下。 – 2010-08-31 17:18:14
客户端验证如何? – 2010-08-31 18:12:52
这肯定会需要自定义客户端验证(这里是众所周知的示例:http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx)。但不知道它将如何与类级验证一起工作。 – 2010-08-31 19:34:13