【问题标题】:Get full property name for MVC client validation for list of objects获取对象列表的 MVC 客户端验证的完整属性名称
【发布时间】:2015-11-04 15:09:15
【问题描述】:

我正在编写一个自定义 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; }

    }
}  

【问题讨论】:

  • 你能展示你应用这个的模型和属性吗
  • @StephenMuecke 编辑显示示例
  • 您不需要(也不应该)在属性中使用 "full name"。假设您使用protected override ValidationResult IsValid(object value, ValidationContext validationContext),那么validationContext 就是Item 模型。我假设您想要的是在您的客户端脚本中获得相应的DependentProperty
  • 用于客户端验证。我从 foolproof 找到了这个解决方案 - 请参阅 MvcFoolproofJQueryValidation.js 文件中的 getName 函数
  • 链接无法正确打开。我认为它可以解决您的问题,但如果没有,我可以为您发布代码。 (您需要获取元素的前缀 - 直到最后一个 . 字符 -,然后附加依赖属性名称并将任何 .[] 字符替换为 _ 给你id 其他属性的属性)

标签: c# asp.net-mvc unobtrusive-validation


【解决方案1】:

验证属性中不需要完全限定的属性名称,并且在任何情况下都无法确定它,因为验证上下文是(在您的情况下)typeof Item(该属性没有父 @ 的上下文987654322@).

您确实需要全名在客户端脚本中(当您将adapter 添加到$.validator.unobtrusive 时。以下脚本将返回依赖属性的idattribute

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

【讨论】:

  • 客户端这就是我链接到的万无一失的库是如何做到的。我想我一直在寻找的答案是你不能,因为该属性没有父级的上下文。
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 2014-11-03
  • 2011-10-03
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多