在我的MVC项目中,我具有不同的自定义验证属性。其中之一是将一个属性的值与另一个属性的值进行比较。如许多文章所述,我添加了类似的内容result.ValidationParameters.Add("otherproperty", _otherPropertyHtml);result.ValidationParameters.Add("comparetype", _compareType);result.ValidationParameters.Add("equalitytype", _equalityType);返回的ModelClientValidationRule对象。我现在的问题是,如果将我要检查的属性封装在另一个对象中,则验证将无法进行。如果我创建类似@Html.TextBoxFor(m => m.ValueOne)@Html.TextBoxFor(m => m.ValueTwo)验证将正常工作,因为它呈现data-val-otherproperty="ValueTwo"我的问题是以下@Html.TextBoxFor(m => m.IntermediateObject.ValueOne)@Html.TextBoxFor(m => m.IntermediateObject.ValueTwo)这将呈现两个名称为IntermediateObject_ValueOne和IntermediateObject.ValueTwo的文本框。但第一个文本框仍为data-val-otherproperty =“ValueOne”。如何实现data-val-otherproperty始终具有其他属性的正确名称?我的想法是类似HtmlHelper 。NameFor(m => ...)或使用反射的东西? 更新1-根据注释的要求添加了代码[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)]public class CustomCompareToOther : ValidationAttribute, IClientValidatable{ // private backing-field private readonly string _otherPropertyName; // constructor public OemCompareToOther(string otherPropertyName) { _otherPropertyName = otherPropertyName; } // implementation of IClientValidatable public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var result = new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.DisplayName), ValidationType = "customcomparetoother" }; // add the property-name so it is known when rendered for client-side validation result.ValidationParameters.Add("otherproperty", _otherPropertyHtml); // here I would need IntermediateObject.ValueTwo instead of only ValueTwo yield return result; }}在模型级别的用法是public class MyModel{ [CustomCompareToOther("ValueOTwo", CompareType.NotEqual, PropertyType.String)] public string ValueOne { get; set; } [CustomCompareToOther("ValueTwo", CompareType.NotEqual, PropertyType.String)] public string ValueTwo { get; set; }}我将在 View 中输入的内容类似于public class ViewModel{ public MyModel IntermediateObject { get; set; }}用过的return View(new ViewModel())。因此,在呈现的HTML中,我将输入<input type="text" name="IntermediateObject_ValueOne" id="IntermediateObject.ValueOne" data-val-customcomparetoother-otherpropertyname="ValueTwo" /><input type="text" name="IntermediateObject_ValueTwo" id="IntermediateObject.ValueTwo" data-val-customcomparetoother-otherpropertyname="ValueOne" />但是我需要<input type="text" name="IntermediateObject_ValueOne" id="IntermediateObject.ValueOne" data-val-customcomparetoother-otherpropertyname="IntermediateObject.ValueTwo" /><input type="text" name="IntermediateObject_ValueTwo" id="IntermediateObject.ValueTwo" data-val-customcomparetoother-otherpropertyname="IntermediateObject.ValueOne" />在html中,以便javascript-validation可以正确获取其他属性。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您可以使用[Compare("PropertyName")]数据注释。 View 模型中的示例:[Display(Name = "New Password")][DataType(DataType.Password)]public string NewPassword { get; set; }[Display(Name = "Confirm Password")][DataType(DataType.Password)][Compare("NewPassword")]public string PasswordConfirmation { get; set; }只要记住要在您的using语句中添加System.ComponentModel.DataAnnotations命名空间 (adsbygoogle = window.adsbygoogle || []).push({}); 10-06 07:53