简体   繁体   English

MVC5中的多个验证要求

[英]Multiple validation requirements in MVC5

I have this form class 'Value' which is used multiple times, the 'Maximum' field is only used in certain instances. 我有这个表单类“ Value”,它被多次使用,“ Maximum”字段仅在某些实例中使用。 I need to validate that 'Maximum' has a value if the 'Show' property is true AND the 'Maximum' field is displayed. 如果“显示”属性为true并且显示“最大”字段,则需要验证“最大”是否具有值。 Thanks in advance! 提前致谢!

Model: 模型:

public class Value
{
    public bool Show { get; set; }
    public bool Required { get; set; }
    [RequiredIf("Show", Domain.Comparison.IsEqualTo, true, ErrorMessageResourceType = typeof(Resources.ErrorMessages), ErrorMessageResourceName = "AuthorLabelIsRequired")]
    public string Label { get; set; }
    public string Description { get; set; }
    [RequiredIf("Show", Domain.Comparison.IsEqualTo, true, ErrorMessageResourceType = typeof(Resources.ErrorMessages), ErrorMessageResourceName = "AuthorLabelIsRequired")]
    public int? Maximum { get; set; }
} 

HTML: HTML:

<tr>
                    <td>@Html.EnumString(Model.Data.DateSection.Key)</td>
                    <td>@Html.CheckBoxFor(m => m.Data.DateSection.Value.Show)</td>
                    <td>@Html.CheckBoxFor(m => m.Data.DateSection.Value.Required)</td>
                    <td>
                        @Html.TextBoxFor(m => m.Data.DateSection.Value.Label, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Data.DateSection.Value.Label)
                    </td>
                    <td>@Html.TextBoxFor(m => m.Data.DateSection.Value.Description, new { @class = "form-control" })</td>
                    <td></td>
                </tr>
                <tr>
                    <td>@Html.EnumString(Model.Data.DescriptionSection.Key)</td>
                    <td>@Html.CheckBoxFor(m => m.Data.DescriptionSection.Value.Show)</td>
                    <td>@Html.CheckBoxFor(m => m.Data.DescriptionSection.Value.Required)</td>
                    <td>
                        @Html.TextBoxFor(m => m.Data.DescriptionSection.Value.Label, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Data.DescriptionSection.Value.Label)
                    </td>
                    <td>@Html.TextBoxFor(m => m.Data.DescriptionSection.Value.Description, new { @class = "form-control" })</td>
                    <td>
                        @Html.TextBoxFor(m => m.Data.DescriptionSection.Value.Maximum, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Data.DescriptionSection.Value.Maximum)
                    </td>
                </tr>

There's actually a rather elegant way to handle conditional validation in ASP.NET MVC. 实际上,在ASP.NET MVC中有一种相当优雅的方法来处理条件验证。 Your model would end up looking something like this: 您的模型最终看起来像这样:

public class Value : IValidatableObject {
    public bool Show { get; set; }
    public bool Required { get; set; }
    [RequiredIf("Show", Domain.Comparison.IsEqualTo, true, ErrorMessageResourceType = typeof(Resources.ErrorMessages), ErrorMessageResourceName = "AuthorLabelIsRequired")]
    public string Label { get; set; }
    public string Description { get; set; }
    [RequiredIf("Show", Domain.Comparison.IsEqualTo, true, ErrorMessageResourceType = typeof(Resources.ErrorMessages), ErrorMessageResourceName = "AuthorLabelIsRequired")]
    public int? Maximum { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        if (this.Show && !this.Maximum.HasValue) {
            yield return new ValidationResult("You must specify a maximum value");
        }
    }
}

Notice how the model: 注意模型如何:

  1. Implements IValidateableObject 实现IValidateableObject
  2. Has a method named Validate which returns the type IEnumerable<ValidationResult> 有一个名为Validate的方法,该方法返回IEnumerable<ValidationResult>类型

During the model binding process this method will automatically be called and if a validation result is returned your ModelState will no longer be valid. 在模型绑定过程中,将自动调用此方法,并且如果返回验证结果,则您的ModelState将不再有效。 So using this familiar code in your controller will make sure you don't take any action unless your custom conditions check out: 因此,在控制器中使用此熟悉的代码将确保您不执行任何操作,除非您的自定义条件签出:

public class SomeController {
    public ActionResult SomeAction() {
        if (ModelState.IsValid) {
            //Do your stuff!
        }
    }
}

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

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