MVC3 ValidationAttribute validationContext.memberName 为 null
我到处都找到了一半的解决方案,但我无法得到真正的答案。
我创建了一个继承自 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 或任何与之交互的东西,它非常基本,但是有一些额外的细节 - 此视图代码位于视图模板中,该模板在部分视图中使用
I find half solutions everywhere but I cannot get a real answer for this.
I have created a validation attribute inheriting from ValidationAttribute and IClientValidatable.
Everything seems to work fine, and if I put a validation summary on the page the error displays.
however I cannot get the error displaying beside the textbox relating to the property.
I think this is because I need to add the property name to the validation result.
HOWEVER this is where it fails, because I cannot get the member name from the validationContext.
is there a way to enable this to happen? Or am I missing something?
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;
}
EDIT/UPDATE
the view looks like this -
<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>
There no javascript or anything interacting with this, its pretty basic however some extra details -
this view code is in a view template, which is used IN a partial view
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先检查模型中的属性是否正确定义了
AdmissionDayCaseAttribute
。然后返回
validationContext.DisplayName
,而不是返回validationContext.MemberName
。如果您通过DsiplayAttribute
修改成员名称,您可以执行以下操作:First check that the
AdmissionDayCaseAttribute
is correctly definied on the property in your model.Then instead of returning
validationContext.MemberName
returnvalidationContext.DisplayName
. If you modified the member name through theDsiplayAttribute
the you could do something like this :