Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取对象列表的MVC客户端验证的完整属性名_C#_Asp.net Mvc_Unobtrusive Validation - Fatal编程技术网

C# 获取对象列表的MVC客户端验证的完整属性名

C# 获取对象列表的MVC客户端验证的完整属性名,c#,asp.net-mvc,unobtrusive-validation,C#,Asp.net Mvc,Unobtrusive Validation,我正在编写一个自定义MVC验证属性,该属性依赖于模型中的另一个命名属性。实现iclientvalidable我的代码如下所示: public IEnumerable<ModelClientValidationRule> GetClientValidationRules (ModelMetadata metadata, ControllerContext context) { var name = metadata.Display

我正在编写一个自定义MVC验证属性,该属性依赖于模型中的另一个命名属性。实现
iclientvalidable
我的代码如下所示:

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;
    }
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; }

    }
}  

验证属性中不需要完全限定的属性名,并且在任何情况下都无法确定它,因为验证上下文(在您的情况下)为
项的类型(该属性没有父
MyClass的上下文)

您确实需要全名的地方在客户端脚本中(当您将
适配器
添加到
$.validator.unobtrusive
时)。以下脚本将返回从属属性的
id
属性

myValidationNamespace={
GetDependentPropertyId:函数(validationElement,DependentProperty){
if(document.getElementById(Dependent属性)){
归还受养人财产;
}
var name=validationElement.name;
var index=name.lastIndexOf(“.”)加1;
DependentProperty=(name.substr(0,索引)+DependentProperty)。替换(/[\.\[\]]]/g,“\”;
if(document.getElementById(Dependent属性)){
归还受养人财产;
}
返回null;
}
}
然后,您可以在初始化客户端验证时使用它

$.validator.unobtrusive.adapters.add(“mycustomvalidation”[“dependentproperty”]),函数(选项){
var元素=options.element;
var dependentproperty=options.params.dependentproperty;
dependentproperty=myValidationNamespace.GetDependentPropertyId(元素,dependentproperty);
选项.规则['mycustomvalidation']={
dependentproperty:dependentproperty
};
options.messages['mycustomvalidation']=options.message;
});

您能否显示应用此功能的模型和属性to@StephenMuecke已编辑,显示示例您不(也不应该)需要属性中的“全名”。假设您的using
protected override ValidationResult有效(对象值,ValidationContext ValidationContext)
,然后,
validationContext
模型。我假设您想要的是在客户端脚本中获得相应的
DependentProperty
?它是用于客户端验证的。我从中找到了此解决方案-请参阅MVCFoolProof JQueryValidation.js文件中的getName函数链接无法正确打开对我来说。我假设它解决了您的问题,但如果没有,我可以为您发布代码。(您需要获取元素的前缀-直到最后一个
字符-,然后附加从属属性名称并替换任何
[
]
字符,带有
以提供另一个属性的
id
属性)客户端,这就是我链接到的万无一失的库所做的。我想我要寻找的答案是,你不能,因为该属性没有父属性的上下文。