问题描述
我有一个自定义验证属性,我正在从asp.net转移到asn.net-core.这是实现IClientModelValidator的一个简单的requiredif属性;
I have a custom validation attribute that i'm moving across from asp.net to asn.net-core. It's a simple requiredif attribute implementing IClientModelValidator;
public class RequiredIfAttribute : ValidationAttribute, IClientModelValidator
{
private String PropertyName { get; set; }
private Object DesiredValue { get; set; }
private readonly RequiredAttribute _innerAttribute;
public RequiredIfAttribute(String propertyName, Object desiredvalue)
{
PropertyName = propertyName;
DesiredValue = desiredvalue;
_innerAttribute = new RequiredAttribute();
}
protected override ValidationResult IsValid(object value, ValidationContext context)
{
var dependentValue = context.ObjectInstance.GetType().GetProperty(PropertyName).GetValue(context.ObjectInstance, null);
if (dependentValue.ToString() == DesiredValue.ToString())
{
if (!_innerAttribute.IsValid(value))
{
return new ValidationResult(FormatErrorMessage(context.DisplayName), new[] { context.MemberName });
}
}
return ValidationResult.Success;
}
public void AddValidation(ClientModelValidationContext context)
{
context.Attributes.Add("data-val", "true");
context.Attributes.Add("data-val-requiredif", ErrorMessage);
//this following line is the issue
context.Attributes.Add("data-val-requiredif-dependentproperty", (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(PropertyName));
context.Attributes.Add("data-val-requiredif-desiredvalue", DesiredValue.ToString());
}
}
正如我在AddValidation方法内的代码行上方注释过的那样,我似乎无法通过将上下文转换为ViewContext并以这种方式访问其名称来获得完整的html字段ID,就像我以前在asp.net中使用的那样.
As i've commented above the line within the AddValidation method, I can't seem to get the full html field id like I used to in asp.net by casting the context to a ViewContext and accessing it's name that way.
获取不是当前上下文的属性的完整html字段ID的新方法是什么?
What is the new way of getting the full html field id of a property that's not the current context?
例如旧代码;
(context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(PropertyName)
可能已返回诸如"viewModel.ComplexObject.PropertyID"之类的路径.
Might have returned a path such as "viewModel.ComplexObject.PropertyID".
我尝试在上下文的ModelMetaData.ContainerContext中查找,但是我不能保证属性容器包含我需要的属性(可以嵌套在其中的其他位置).
I've tried looking in the context's ModelMetaData.ContainerContext But I can't guarantee that the properties container contains the property I need (it could be nested elsewhere within that).
例如,假设我的模型看起来像这样;
For example, let's assume that my model looks like so;
public class A
{
[RequiredIf("PropertyB", true)]
public string propertyA { get; set; }
public bool PropertyB { get; set; }
}
有什么想法吗?
推荐答案
在 AddValidation 方法中使用以下代码:
Use following code in AddValidation method:
var viewContext = context.ActionContext as ViewContext viewContext;
var fullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(PropertyName);
var fullId = propertyFullName.Replace(".", "_");
注意:TemplateInfo现在在asp.net核心中没有 GetFullHtmlFieldId 方法
Note: TemplateInfo now not have GetFullHtmlFieldId Method in asp.net core
这篇关于从ClientModelValidationContext访问完整的html字段属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!