Asp.net mvc 根据MVC模型中的其他字段验证所需字段

Asp.net mvc 根据MVC模型中的其他字段验证所需字段,asp.net-mvc,validation,model,Asp.net Mvc,Validation,Model,我想创建一个模型,该模型将根据其他字段条件验证模型中的必填字段 public class FixedDeposit { public int DebitAmount { get; set; } public string PAN { get; set; } } 现在,如果DebitAmount大于50000,则必须使用PAN字段 您可以实现IValidatableObject public class FixedDeposit : IValidatableObject { p

我想创建一个模型,该模型将根据其他字段条件验证模型中的必填字段

public class FixedDeposit
{
   public int DebitAmount { get; set; }
   public string PAN { get; set; }
}

现在,如果DebitAmount大于50000,则必须使用PAN字段

您可以实现IValidatableObject

public class FixedDeposit : IValidatableObject
{
   public int DebitAmount { get; set; }
   public string PAN { get; set; }

   public IEnumerable<ValidationResult> Validate(ValidationContext   validationContext)
   {
      if (DebitAmount > 50000 && string.IsNullOrEmpty(PAN))
      {
        yield return new ValidationResult("PAN required for debits > 50,000.", new [] { "PAN" } );
      }
   }
}
public类FixedDeposit:IValidatableObject
{
公共int DebitAmount{get;set;}
公共字符串PAN{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext)
{
if(DebitAmount>50000&&string.IsNullOrEmpty(PAN))
{
返回新的ValidationResult(“借记>50000需要PAN.”,新[]{“PAN”});
}
}
}

您可以实现IValidatableObject

public class FixedDeposit : IValidatableObject
{
   public int DebitAmount { get; set; }
   public string PAN { get; set; }

   public IEnumerable<ValidationResult> Validate(ValidationContext   validationContext)
   {
      if (DebitAmount > 50000 && string.IsNullOrEmpty(PAN))
      {
        yield return new ValidationResult("PAN required for debits > 50,000.", new [] { "PAN" } );
      }
   }
}
public类FixedDeposit:IValidatableObject
{
公共int DebitAmount{get;set;}
公共字符串PAN{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext)
{
if(DebitAmount>50000&&string.IsNullOrEmpty(PAN))
{
返回新的ValidationResult(“借记>50000需要PAN.”,新[]{“PAN”});
}
}
}

您还可以使用MVC万无一失的验证包。这个包以注释的形式为您提供了许多条件验证

完整列表如下:

您可以将此库作为包添加到VS项目中:

对于FixedPayment类,它应该如下所示:

using Foolproof;
public class FixedDeposit
{
    public int DebitAmount { get; set; }

    [RequiredIf("DebitAmount", Operator.GreaterThan, 50000)]
    public string PAN { get; set; }
}
备用代码

using Foolproof;
public class FixedDeposit
{
    public int DebitAmount { get; set; }

    private bool _panRequired { get { return DebitAmount > 50000; } }
    [RequiredIf("_panRequired", true, ErrorMessage="PAN is required if Debit Amount is greater than 50000")]
    public string PAN { get; set; }
}

您还可以使用MVC万无一失的验证包。这个包以注释的形式为您提供了许多条件验证

完整列表如下:

您可以将此库作为包添加到VS项目中:

对于FixedPayment类,它应该如下所示:

using Foolproof;
public class FixedDeposit
{
    public int DebitAmount { get; set; }

    [RequiredIf("DebitAmount", Operator.GreaterThan, 50000)]
    public string PAN { get; set; }
}
备用代码

using Foolproof;
public class FixedDeposit
{
    public int DebitAmount { get; set; }

    private bool _panRequired { get { return DebitAmount > 50000; } }
    [RequiredIf("_panRequired", true, ErrorMessage="PAN is required if Debit Amount is greater than 50000")]
    public string PAN { get; set; }
}

您可以使用两个选项

第一个是Jaroslaw Waliszko开发的非常易于使用且非常简洁的
ExpressiveAnnotations
JS库。有关更多信息,请访问此链接。此库允许您执行不同的条件验证。 与此类似,它通过添加NuGet包添加到VisualStudio环境中。添加后,在模型中添加using语句
using ExpressiveAnnotations.Attributes
然后只需使用
RequiredIf
声明即可完成所需操作。例如:

public class FixedDeposit
{
   public int DebitAmount { get; set; }

   [RequiredIf("DebitAmount >= 50000")]
   public string PAN { get; set; }
}
第二个选项是使用
ModelState.addmodeleror()
。这是在您的控制器内完成的。只需创建一个新方法:

private void ValidateRequiredFields(modelname)
{
    if(modelname.DebitAmount >= 50000)
    {
        if(modelname.PAN == null)
        {
            ModelState.AddModelError("PAN", "Place whatever error message you want here");
        }
    }
}

接下来,在希望调用的任何视图方法中放置对验证方法的引用。要引用的行是
ValidateRequiredFields(ModelName)

您可以使用两个选项

第一个是Jaroslaw Waliszko开发的非常易于使用且非常简洁的
ExpressiveAnnotations
JS库。有关更多信息,请访问此链接。此库允许您执行不同的条件验证。 与此类似,它通过添加NuGet包添加到VisualStudio环境中。添加后,在模型中添加using语句
using ExpressiveAnnotations.Attributes
然后只需使用
RequiredIf
声明即可完成所需操作。例如:

public class FixedDeposit
{
   public int DebitAmount { get; set; }

   [RequiredIf("DebitAmount >= 50000")]
   public string PAN { get; set; }
}
第二个选项是使用
ModelState.addmodeleror()
。这是在您的控制器内完成的。只需创建一个新方法:

private void ValidateRequiredFields(modelname)
{
    if(modelname.DebitAmount >= 50000)
    {
        if(modelname.PAN == null)
        {
            ModelState.AddModelError("PAN", "Place whatever error message you want here");
        }
    }
}
接下来,在希望调用的任何视图方法中放置对验证方法的引用。要引用的行是
ValidateRequiredFields(ModelName)

用法

用法


我已经安装了MVC万无一失的验证包。并使用与您提到的相同的语法,但除此之外,所有其他验证都在工作。我用另一个实现编辑了我的答案。我在我自己的应用程序中使用了与此选项几乎完全相同的东西。工作正常。谢谢。我已经安装了MVC万无一失的验证包。并使用与您提到的相同的语法,但除此之外,所有其他验证都在工作。我用另一个实现编辑了我的答案。我在我自己的应用程序中使用了与此选项几乎完全相同的东西。工作正常。谢谢。这是正确答案,无需向应用程序中添加额外程序包。这是正确答案,无需向应用程序中添加额外程序包