查找GetClientValidationRules复杂对象的属

查找GetClientValidationRules复杂对象的属

本文介绍了查找GetClientValidationRules复杂对象的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是需要在客户机上进行验证的复合视图模型。简化的:

There is a complex view model that needs to be validated on the client. Simplified:

public class Survey
{
  public List<Question> Questions { get; set; }
}

public class Question
{
  public List<Answer> Answers { get; set; }
}

public class Answer
{
  public string FieldName { get; set; }

  [Required("Please use a number")]
  public int Number { get; set; }
}

目前,问题正在正确验证客户端。但是,我们需要验证,并使用字段名像显示上下文信息:

Currently, the question is being validated correctly on the client. But we need to validate and display a contextual message using FieldName like:

是必需的字段的儿童人数。

我实现了一个 CustomRequiredAttribute 类(:RequiredAttribute标签,IClientValidatable )和装饰号码与它属性:

I implemented a CustomRequiredAttribute class (: RequiredAttribute, IClientValidatable) and decorated the Number property with it:

[CustomRequired("{0} is mandatory")]
public int Number { get; set; }

不过,在 GetClientValidationRules 法,metadata.Model为null。

but, in the GetClientValidationRules method, the metadata.Model is null.

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
              ModelMetadata metadata,
              ControllerContext context)
{
  // metadata.Model is null here!
  ModelMetadata answerFieldNameValue = ModelMetadataProviders.Current
                                        .GetMetadataForProperties(
                                              metadata.Model,
                                              metadata.ContainerType)
                        .FirstOrDefault(p => p.PropertyName == "FieldName");

  // and, therefore, answerFieldNameValue.Model is null too!
}

如果我回到这个方法的第一行,并从 context.Controller.ViewData.Model 我得到任何并将其分配给 metadata.Model ,那么 answerFieldNameValue.Model 将包含正确的字符串,它是答案的字段名。

If I go back to the first line of this method and from the context.Controller.ViewData.Model I get any Answer and assign it to metadata.Model, then the answerFieldNameValue.Model will contain the proper string which is the field name of the answer.

如何让这个metadata.Model有一个适当的值时,它得到吗?或任何其他方式来解决这个问题?

How to make this metadata.Model have a proper value when it gets here? Or any other way to solve this problem?

推荐答案

明白了。下面的code是我的解决方案这是由。

Got it. The code below is my solution which was inspired by BranTheMan who found his way out of a similar problem.

public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    private object lastQuestionLabel;

    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        ModelMetadata modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        object model = null;

        if (modelAccessor != null)
        {
            model = modelAccessor();
        }

        if (typeof(SurveyQuestionVM).IsAssignableFrom(containerType) && propertyName == "TreeViewLabel")
        {
            lastQuestionLabel = model;
        }

        if (typeof(SurveyAnswerVM).IsAssignableFrom(containerType))
        {
            modelMetadata.AdditionalValues.Add("QuestionLabel", lastQuestionLabel);
        }

        return modelMetadata;
    }
}

这篇关于查找GetClientValidationRules复杂对象的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:13