获取对象列表的 MVC 客户端验证的完整 属性 名称
Get full property name for MVC client validation for list of objects
我正在编写一个自定义 MVC 验证属性,该属性依赖于模型中另一个名为 属性 的属性。实施 IClientValidatable
我的代码如下所示:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules
(ModelMetadata metadata, ControllerContext context)
{
var name = metadata.DisplayName ?? metadata.PropertyName;
ModelClientValidationRule rule = new ModelClientValidationRule()
{ ErrorMessage = FormatErrorMessage(name), ValidationType = "mycustomvalidation" };
rule.ValidationParameters.Add("dependentproperty", dependentProperty);
yield return rule;
}
问题是我试图在元素列表中使用它。从属 属性 在名称为 MyListOfObjects[0].DependentProperty
的视图中呈现,验证规则呈现为 data-val-mycustomvalidation-dependentproperty="DependentProperty"
如何从 GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
中访问受抚养人 属性 的全名,使其呈现为 data-val-mycustomvalidation-dependentproperty="MyListOfObjects[0].DependentProperty"
模型看起来像这样:
public class MyClass
{
public string SomeValue { get; set; }
public List<Item> MyListOfObjects { get; set; }
public class Item
{
[MyCustomValidation("DependentProperty")]
public int MyValidatedElement { get; set; }
public int DependentProperty { get; set; }
}
}
您不需要验证属性中的完全限定 属性 名称,并且在任何情况下都无法确定它,因为验证上下文(在您的情况下)是 typeof Item
(属性没有父 MyClass
).
的上下文
您需要全名的地方是在客户端脚本中(当您将 adapter
添加到 $.validator.unobtrusive
时)。以下脚本将 return id
依赖项的属性 属性
myValidationNamespace = {
getDependantProperyID: function (validationElement, dependantProperty) {
if (document.getElementById(dependantProperty)) {
return dependantProperty;
}
var name = validationElement.name;
var index = name.lastIndexOf(".") + 1;
dependantProperty = (name.substr(0, index) + dependantProperty).replace(/[\.\[\]]/g, "_");
if (document.getElementById(dependantProperty)) {
return dependantProperty;
}
return null;
}
}
然后您可以在初始化客户端验证时使用它
$.validator.unobtrusive.adapters.add("mycustomvalidation", ["dependentproperty"], function (options) {
var element = options.element;
var dependentproperty = options.params.dependentproperty;
dependentproperty = myValidationNamespace.getDependantProperyID(element, dependentproperty);
options.rules['mycustomvalidation'] = {
dependentproperty: dependentproperty
};
options.messages['mycustomvalidation'] = options.message;
});
我正在编写一个自定义 MVC 验证属性,该属性依赖于模型中另一个名为 属性 的属性。实施 IClientValidatable
我的代码如下所示:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules
(ModelMetadata metadata, ControllerContext context)
{
var name = metadata.DisplayName ?? metadata.PropertyName;
ModelClientValidationRule rule = new ModelClientValidationRule()
{ ErrorMessage = FormatErrorMessage(name), ValidationType = "mycustomvalidation" };
rule.ValidationParameters.Add("dependentproperty", dependentProperty);
yield return rule;
}
问题是我试图在元素列表中使用它。从属 属性 在名称为 MyListOfObjects[0].DependentProperty
的视图中呈现,验证规则呈现为 data-val-mycustomvalidation-dependentproperty="DependentProperty"
如何从 GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
中访问受抚养人 属性 的全名,使其呈现为 data-val-mycustomvalidation-dependentproperty="MyListOfObjects[0].DependentProperty"
模型看起来像这样:
public class MyClass
{
public string SomeValue { get; set; }
public List<Item> MyListOfObjects { get; set; }
public class Item
{
[MyCustomValidation("DependentProperty")]
public int MyValidatedElement { get; set; }
public int DependentProperty { get; set; }
}
}
您不需要验证属性中的完全限定 属性 名称,并且在任何情况下都无法确定它,因为验证上下文(在您的情况下)是 typeof Item
(属性没有父 MyClass
).
您需要全名的地方是在客户端脚本中(当您将 adapter
添加到 $.validator.unobtrusive
时)。以下脚本将 return id
依赖项的属性 属性
myValidationNamespace = {
getDependantProperyID: function (validationElement, dependantProperty) {
if (document.getElementById(dependantProperty)) {
return dependantProperty;
}
var name = validationElement.name;
var index = name.lastIndexOf(".") + 1;
dependantProperty = (name.substr(0, index) + dependantProperty).replace(/[\.\[\]]/g, "_");
if (document.getElementById(dependantProperty)) {
return dependantProperty;
}
return null;
}
}
然后您可以在初始化客户端验证时使用它
$.validator.unobtrusive.adapters.add("mycustomvalidation", ["dependentproperty"], function (options) {
var element = options.element;
var dependentproperty = options.params.dependentproperty;
dependentproperty = myValidationNamespace.getDependantProperyID(element, dependentproperty);
options.rules['mycustomvalidation'] = {
dependentproperty: dependentproperty
};
options.messages['mycustomvalidation'] = options.message;
});