MVC3 ValidationAttribute validationContext.memberName为null

时间:2012-02-21 03:05:24

标签: c# asp.net visual-studio asp.net-mvc-3 validation

我到处找到一半的解决方案,但我无法得到真正的答案。

我创建了一个继承自ValidationAttribute和IClientValidatable的验证属性。

一切似乎都运行良好,如果我在页面上放置验证摘要,则显示错误。

但是我无法在与该属性相关的文本框旁边显示错误。

我认为这是因为我需要将属性名称添加到验证结果中。

然而,这是失败的地方,因为我无法从validationContext获取成员名称。

有没有办法让这种情况发生?或者我错过了什么?

public class AdmissionDayCaseAttribute : ValidationAttribute, IClientValidatable
{
    private String DayCaseID;
    private String AdmissionDateID;
    private String DischargeDateID;

    public AdmissionDayCaseAttribute(String DayCaseID, String AdmissionDateID, String DischargeDateID)
    {
        this.DayCaseID = DayCaseID;
        this.AdmissionDateID = AdmissionDateID;
        this.DischargeDateID = DischargeDateID;
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        object AdmissionDate = GetPropertyValue(validationContext.ObjectInstance, AdmissionDateID);
        object DischargeDate = GetPropertyValue(validationContext.ObjectInstance, DischargeDateID);
        object AdmissionType = validationContext.MemberName == null ? null : GetPropertyValue(validationContext.ObjectInstance, validationContext.MemberName);
        if (AdmissionDate != null && DischargeDate != null && AdmissionType != null)
        {
            if (AdmissionDate.Equals(DischargeDate) && DayCaseID.Equals(AdmissionType.ToString()))
            {
                return new ValidationResult(ErrorMessage, new List<String>() { validationContext.MemberName });
            }
        }
        return ValidationResult.Success;
    }
    private object GetPropertyValue(object instance,String propertyName)
    {
        Type itype = instance.GetType();
        return itype.GetProperty(propertyName).GetValue(instance,null);
    }
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule vr = new ModelClientValidationRule()
        {
            ErrorMessage = ErrorMessage,
            ValidationType = "admissiondaycase"
        };
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("admissiondateid", AdmissionDateID));
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("dischargedateid", DischargeDateID));
        vr.ValidationParameters.Add(new KeyValuePair<string, object>("daycaseid", DayCaseID));
        yield return vr;
    }

修改/更新

视图看起来像这样 -

<tr>
    <td>
        @Html.LabelFor(x => x.AdmissionTypeID)
    </td>
    <td>
        @Html.CustomDropDownListFor(a => a.AdmissionTypeID, Lookup.AdmissionTypes,Default.Constant.DROPDOWN)
        @Html.ValidationMessageFor(a=>a.AdmissionTypeID)
    </td>
</tr>

没有javascript或任何与此相互作用的东西,它非常基本但是一些额外的细节 - 此视图代码位于视图模板中,在部分视图中使用

1 个答案:

答案 0 :(得分:7)

首先检查模型中属性的AdmissionDayCaseAttribute是否正确定义。

然后返回validationContext.MemberName而不是返回validationContext.DisplayName。如果您通过DsiplayAttribute修改了成员名称,则可以执行以下操作:

  var memberName = validationContext.ObjectType.GetProperties()
    .Where(p => p.GetCustomAttributes(false)
    .OfType<DisplayAttribute>()
    .Any(a => a.Name == validationContext.DisplayName))
    .Select(p => p.Name).FirstOrDefault(); 

    return new ValidationResult(ErrorMessage, new List<String>() { memberName  });