MVC5中的多个验证需求

本文关键字:验证 需求 MVC5 | 更新日期: 2023-09-27 18:09:08

我有这个表单类'Value'被多次使用,'Maximum'字段仅在某些情况下使用。我需要验证"最大"有一个值,如果"显示"属性为真,并显示"最大"字段。提前感谢!

模型:

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:

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

MVC5中的多个验证需求

实际上在ASP中有一种相当优雅的方式来处理条件验证。净MVC。你的模型最终看起来像这样:

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

注意模型:

  1. 实现IValidateableObject
  2. 有一个名为Validate的方法,返回类型IEnumerable<ValidationResult>

在模型绑定过程中,此方法将被自动调用,如果返回验证结果,您的ModelState将不再有效。所以在你的控制器中使用这段熟悉的代码将确保你不会采取任何行动,除非你的自定义条件检查出来:

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