C# NETMVC。如何基于参数禁用所需的验证?

C# NETMVC。如何基于参数禁用所需的验证?,c#,asp.net,asp.net-mvc,validation,requiredfieldvalidator,C#,Asp.net,Asp.net Mvc,Validation,Requiredfieldvalidator,我确实有一个实体类,根据选择器的不同,它具有一些必需的属性 例如:选择器可以假定为“1”或“2”。如果选择器为“1”,则需要一组参数。如果选择器为“2”,则需要另一组参数 class MyClass{ public int Selector {get;set;} // 1 or 2 public string A_required_for_1 {get;set;} public string B_required_for_1 {get;set;} publi

我确实有一个实体类,根据选择器的不同,它具有一些必需的属性

例如:选择器可以假定为“1”或“2”。如果选择器为“1”,则需要一组参数。如果选择器为“2”,则需要另一组参数

class MyClass{

    public int Selector {get;set;} // 1 or 2

    public string A_required_for_1 {get;set;}
    public string B_required_for_1 {get;set;}

    public string C_required_for_2 {get;set;}
    public string D_required_for_2 {get;set;}

    public string E_Required_for_both_selectors {get;set;}

}
在视图中创建或编辑操作期间,用户应该能够在选择器之间切换

客户端验证已经解决


我如何在服务器验证中处理它?

我相信mvcfoolproof将适用于这种情况[ 它也可以在nuget上使用

[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]

它的使用非常简单。

您可以创建自己的自定义验证属性,也可以使用,然后执行以下操作:

class MyClass
{

    public int Selector {get;set;} // 1 or 2

    [RequiredIf("Selector == 1", ErrorMessage = "Your Error Message")]
    public string A_required_for_1 {get;set;}

    [RequiredIf("Selector == 1", ErrorMessage = "Your Error Message")]
    public string B_required_for_1 {get;set;}

    [RequiredIf("Selector == 2", ErrorMessage = "Your Error Message")]
    public string C_required_for_2 {get;set;}

    [RequiredIf("Selector == 2", ErrorMessage = "Your Error Message")]
    public string D_required_for_2 {get;set;}

    [Required("Your Error Message")]
    public string E_Required_for_both_selectors {get;set;}

 }
正如Win所提到的,它似乎已经有一段时间没有进行过积极的开发,因此您可能希望继续创建自己的自定义验证属性,这确实需要更多的工作,但您可以更好地控制验证本身。根据需要进行选择

对于自定义验证属性,可以执行以下操作:

public class RequiredIfOtherProperty : ValidationAttribute
{
    private readonly string _otherPropertyName;
    private readonly string _compareValue;

    public RequiredIfOtherProperty(string otherPropertyName, string compareValue)
    {
        _otherPropertyName = otherPropertyName;
        _compareValue = compareValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherProperty = validationContext.ObjectType.GetProperty(_otherPropertyName);
        if (otherProperty == null)
        {
            return new ValidationResult($"Property '{_otherPropertyName}' does not exist");
        );

        var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);
        if (!_compareValue.Equals(otherPropertyValue))
        {
            return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }
}
它应该能让你大致了解你能做什么,你可以把实际的验证改成你喜欢的方式。然后你可以像使用普通属性一样使用它,例如

[RequiredIfOtherProperty("SomeProperty", "ValueToCompareWith")]

它不是“在某些情况下禁用必需的验证属性”重复请帮自己一个忙,在应用程序中单独使用编辑和创建功能。@NicholasKinney我不明白您为什么要将注释应用于该问题。请您澄清一下,好吗?如果您正在使用一个类型错误的对象强式键入视图,那么您将希望在编辑视图和创建视图之间分离对象。我希望e这很有帮助。希望这能解决您的问题。不错的库,但它是Beta 0.9.4517,自2012年5月14日以来未更新。不错的库,但它是Beta 0.9.4517,自2012年5月14日以来未更新。您所说的“自定义验证属性”是什么意思?