ASP.NET MVC 条件验证 [英] ASP.NET MVC Conditional validation

查看:25
本文介绍了ASP.NET MVC 条件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用数据注解对模型进行条件验证?

How to use data annotations to do a conditional validation on model?

例如,假设我们有以下模型(Person 和 Senior):

For example, lets say we have the following model (Person and Senior):

public class Person
{
    [Required(ErrorMessage = "*")]
    public string Name
    {
        get;
        set;
    }

    public bool IsSenior
    {
        get;
        set;
    }

    public Senior Senior
    {
        get;
        set;
    }
}

public class Senior
{
    [Required(ErrorMessage = "*")]//this should be conditional validation, based on the "IsSenior" value
    public string Description
    {
        get;
        set;
    }
}

以及以下视图:

<%= Html.EditorFor(m => m.Name)%>
<%= Html.ValidationMessageFor(m => m.Name)%>

<%= Html.CheckBoxFor(m => m.IsSenior)%>
<%= Html.ValidationMessageFor(m => m.IsSenior)%>

<%= Html.CheckBoxFor(m => m.Senior.Description)%>
<%= Html.ValidationMessageFor(m => m.Senior.Description)%>

我想成为基于IsSenior"属性选择的Senior.Description"属性条件必填字段(true -> required).如何使用数据注释在 ASP.NET MVC 2 中实现条件验证?

I would like to be the "Senior.Description" property conditional required field based on the selection of the "IsSenior" propery (true -> required). How to implement conditional validation in ASP.NET MVC 2 with data annotations?

推荐答案

我通过处理 "ModelState" 字典,由控制器包含.ModelState 字典包含所有必须验证的成员.

I have solved this by handling the "ModelState" dictionary, which is contained by the controller. The ModelState dictionary includes all the members that have to be validated.

解决办法如下:

如果您需要基于某个字段实现条件验证(例如,如果 A=true,则需要 B),同时保持属性级别的错误消息(这对于对象级别的自定义验证器而言并非如此)您可以通过处理ModelState"来实现这一点,只需从中删除不需要的验证即可.

If you need to implement a conditional validation based on some field (e.g. if A=true, then B is required), while maintaining property level error messaging (this is not true for the custom validators that are on object level) you can achieve this by handling "ModelState", by simply removing unwanted validations from it.

...在某个班级...

...In some class...

public bool PropertyThatRequiredAnotherFieldToBeFilled
{
  get;
  set;
}

[Required(ErrorMessage = "*")] 
public string DepentedProperty
{
  get;
  set;
}

...课程继续...

...在一些控制器动作...

...In some controller action ...

if (!PropertyThatRequiredAnotherFieldToBeFilled)
{
   this.ModelState.Remove("DepentedProperty");
}

...

这样我们就实现了条件验证,而其他一切都保持不变.

更新:

这是我的最终实现:我在模型上使用了一个接口以及验证实现上述接口的模型的 action 属性.接口规定了 Validate(ModelStateDictionary modelState) 方法.action 上的属性只是调用 IValidatorSomething 上的 Validate(modelState).

This is my final implementation: I have used an interface on the model and the action attribute that validates the model which implements the said interface. Interface prescribes the Validate(ModelStateDictionary modelState) method. The attribute on action just calls the Validate(modelState) on IValidatorSomething.

我不想让这个答案复杂化,所以我没有提到最终的实现细节(最后,这在生产代码中很重要).

I did not want to complicate this answer, so I did not mention the final implementation details (which, at the end, matter in production code).

这篇关于ASP.NET MVC 条件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆