Asp.net mvc 3 使用dataannotation创建的Asp.net MVC3自定义验证器在不正确的位置显示消息

Asp.net mvc 3 使用dataannotation创建的Asp.net MVC3自定义验证器在不正确的位置显示消息,asp.net-mvc-3,data-annotations,customvalidator,Asp.net Mvc 3,Data Annotations,Customvalidator,我正在使用Asp.net MVC3、razor视图引擎和数据注释进行模型验证 我有一个表单,其中必须输入Url详细信息(Url和描述)。这两个字段都不是必需的。但若我输入一个字段,那个么另一个字段必须是必填的。若我输入的描述Url是必填的,并且格式正确,若我输入的是Url,那个么描述是必填的 我为数据注释创建了一个customvalidator。它验证并输出错误消息。 但我的问题是ValidationMessageFor生成的错误消息位于错误的位置。 即,如果输入description,则所需的

我正在使用Asp.net MVC3、razor视图引擎和数据注释进行模型验证

我有一个表单,其中必须输入Url详细信息(Url和描述)。这两个字段都不是必需的。但若我输入一个字段,那个么另一个字段必须是必填的。若我输入的描述Url是必填的,并且格式正确,若我输入的是Url,那个么描述是必填的

我为数据注释创建了一个customvalidator。它验证并输出错误消息。 但我的问题是ValidationMessageFor生成的错误消息位于错误的位置。 即,如果输入description,则所需的url消息是description的一部分。 我希望该消息作为ValidationMessageForURL字段的一部分

有人能帮我吗?提前谢谢。下面是我使用的代码

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]

public sealed class IsExistAttribute : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessage = "{0} is required."; 
public string OtherProperty { get; private set; } 
public IsExistAttribute (string otherProperty)
    : base(DefaultErrorMessage)
{
    if (string.IsNullOrEmpty(otherProperty))
    {
        throw new ArgumentNullException("otherProperty");
    }

    OtherProperty = otherProperty;
}

public override string FormatErrorMessage(string name)
{
    return string.Format(ErrorMessageString, name, OtherProperty);
}

protected override ValidationResult IsValid(object value,ValidationContext validationContext)
{
    if (value != null)
    {
        var otherProperty = validationContext.ObjectInstance.GetType()
                                             .GetProperty(OtherProperty);

        var otherPropertyValue = otherProperty
                                    .GetValue(validationContext.ObjectInstance, null);
        var strvalue=Convert.ToString(otherPropertyValue)

        if (string.IsNullOrEmpty(strvalue))
        {
            //return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName),new[] { OtherProperty});

        }
    }

    return ValidationResult.Success;
 } 

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
{
    var clientValidationRule = new ModelClientValidationRule()
    {
       ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
       ValidationType = "isexist"
    };

   clientValidationRule.ValidationParameters.Add("otherproperty", OtherProperty); 
   return new[] { clientValidationRule };           
}      
}
以及

<div class="editor-field">
@Html.TextBoxFor(m => m.Description )
@Html.ValidationMessageFor(m => m.Description)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Url)
@Html.ValidationMessageFor(m => m.Url)
</div>

你应该反过来进行验证。更改:

if (string.IsNullOrEmpty(otherPropertyValue))
    {
        //return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName),new[] { OtherProperty});

    }
致:

如果(value!=null)


如果另一个字段已填充,那么将失效的字段是空字段。

谢谢您的想法。它似乎在服务器端工作:)但在客户端,如果我实现非结构化javascript验证,在更改空字段时,只有验证在客户端触发。。是否使用非结构化验证逻辑更新
(function ($) {
    $.validator.addMethod("isexist", function (value, element, params) {
        if (!this.optional(element)) {
            var otherProp = $('#' + params)
            return (otherProp.val() !='' &&  value!='');//validation logic--edited by Rajesh 
        }
        return true;
    });
    $.validator.unobtrusive.adapters.addSingleVal("isexist", "otherproperty");                        
} (jQuery));  
if (string.IsNullOrEmpty(otherPropertyValue))
    {
        //return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName),new[] { OtherProperty});

    }
if (string.IsNullOrEmpty(Convert.ToString(value)) && !string.IsNullOrEmpty(otherPropertyValue))
    {
        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }