简体   繁体   English

三层深度模型的自定义验证属性

[英]Custom Validation Attribute for three levels deep model

Hi I have a situation in witch I have to create some custom validation attributes because the way my model is created.The model looks something like this: 嗨,我有一个女巫的情况,我必须创建一些自定义验证属性,因为创建模型的方式如下所示:

public class EvaluationFormDataContract
{
    public int StudentAssignmentInstanceId { get; set; }

    public int EvaluationType { get; set; }


    public List<CategoriesOnEvaluationDataContract> Categories { get; set; }
}

  public class CategoriesOnEvaluationDataContract
{
    public string Memo { get; set; }

    public int CategoryId { get; set; }

    public List<QuestionsOnEvalCategoryDataContract> Questions { get; set; }

    // Fields needed for validation
    public bool? HasMemo { get; set; }

    public bool MemoIsMandatory { get; set; }
}

    public class QuestionsOnEvalCategoryDataContract
{
    public string Memo { get; set; }

    public string Grade { get; set; }

    public int QuestionId { get; set; }

    // Fields needed for validation
    public bool HasGrade { get; set; }

    public bool HasMemo { get; set; }

    public bool ShowOnlyMemo { get; set; }
}

As it can be seem the model is composed two levels deep. 看起来该模型由两个层次组成。 And I will have to validate starting from the second level , where I will check if the model HasMemo and if MemoIsMandatory. 我将必须从第二个级别开始进行验证,在第二个级别中,我将检查模型HasMemo和MemoIsMandatory。

The third validation should be done at the 3rd level where I have to check if it HasGrade and HasMemo. 第三次验证应在第3层完成,我必须检查它是否具有HasGrade和HasMemo。

Normaly if it were up to me I would split this in three separate calls to the server but we are depending on an legacy project and for the moment I have to make this work. 通常,如果由我决定,我会将其分为三个对服务器的单独调用,但是我们依赖于一个旧项目,目前我必须进行这项工作。

The post action will be called via an ajax call and will have all this data into it. 将通过ajax调用来调用post操作,并将所有这些数据放入其中。

Now my question is where should I add the validation attribute? 现在我的问题是我应该在哪里添加验证属性?

Should it be added at the top on Categories , making it directly responsible for all the levels of the model? 是否应该将其添加到Categories的顶部,使其直接负责模型的所有级别?

Or I should place it on each model and find a way to make the data binder aware of it? 或者我应该将其放置在每个模型上,并找到一种使数据绑定器意识到它的方法? If so how can I do this? 如果可以,我该怎么做?

You can do both. 两者都可以。 If you implement System.ComponentModel.DataAnnotations.IValidatableObject interface at the top-most level, you can do whatever you want with the properties in the entire graph and return the errors. 如果在最顶层实现System.ComponentModel.DataAnnotations.IValidatableObject接口,则可以对整个图形中的属性执行任何所需的操作,并返回错误。

public class EvaluationFormDataContract : IValidatableObject
{
        // All properties go here

        public IEnumerable<ValidationResult> Validate(
                                 ValidationContext validationContext)
        {
            if (// do what you want)
                yield return new ValidationResult("message");
        }
}

Or, you can apply attributes at the lower levels and automatically binding takes care of validating the properties in the graph. 或者,您可以在较低级别应用属性,并且自动绑定将负责验证图形中的属性。 You don't need to do anything special. 您不需要做任何特别的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM