自定义验证不会阻止创建操作发布 [英] custom validation doesn't prevent the create action from posting

查看:86
本文介绍了自定义验证不会阻止创建操作发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义验证似乎工作正常,它被命中并且逻辑正在工作但是当IsValid返回false时它仍然转到创建帖子方法



这是我的自定义验证类

  public   class  CheckIfSparesAvailableAttribute:ValidationAttribute 
{
public CheckIfSparesAvailableAttribute(): base ( ){}

public 覆盖 bool IsValid( object value
{
int quantity,spareID =( int value ;

quantity = new 备件().CheckQuantity(spareID);

if (数量< 1
return false ;
return true ;

}
}





这是我的模特

  public   class  IncidentSpares 
{
// 其他属性
[必需]
[显示(名称= 备用)]
[CheckIfSparesAvailable(ErrorMessage = 测试)]
public int SpareID { get ; set ; }
}





PS:这是一个多对多表,我正在尝试验证包含的Incident类IncidentSpare的ICollection

这里是事件模型

  public   class 突发事件
{
// 其他属性
[显示(名称= 备件)]
public virtual ICollection< IncidentSpares> SpareParts { get ; set ; }
}





我在谈论的创建方法是在IncidentController中



[更新]

我试图在客户端进行此工作我已经编写了如下GetClientValidationRules方法



< pre lang =c#> public IEnumerable< ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ValidationType = checkifsparesavailable
ErrorMessage = < span class =code-string> 库存中没有备件
};
yield return 规则;
}





我必须写javascript才能在客户端工作,如果是的话请帮助

解决方案

  public   class  CheckIfSparesAvailableAttribute:ValidationAttribute 
{
public CheckIfSparesAvailableAttribute(): base (){}

public override bool IsValid( object value
{
int 数量,spareID =( int value ;

quantity = new 备件().CheckQuantity(spareID);

if (数量< 1 ){
return new ValidationResult( 请选择一个选项。);

}
else {

return ValidationResult.Success;
}

}
}



并且由于您使用的是validationattribute,这将触及控制器操作方法,并且在action方法中,您需要检查if(ModelState.IsValid)

因为在自定义验证属性中,您正在覆盖IsValid方法。您正在尝试使用 CheckIfSparesAvailableAttribute 作为属性,因为使用了AuthorizationAttribute。因为在这里你要覆盖IsValid方法,只需要按照上面的方式返回并检查ActionState for ModelState.IsValid

我希望你得到我试图解释的内容。

欲了解更多信息。按照: -

在MVC 3中创建自定义验证属性 [ ^ ]



谢谢。


I have a custom validation that seems to work fine, it get hit and the logic is working but when IsValid return false it still go to the create post method

here is my custom validation class

public class CheckIfSparesAvailableAttribute : ValidationAttribute
    {
        public CheckIfSparesAvailableAttribute() : base() { }

        public override bool IsValid(object value)
        {
            int quantity, spareID = (int)value;

            quantity = new Spares().CheckQuantity(spareID);

            if (quantity < 1)
                return false;
            return true;
            
        }
    }



and here is my model

public class IncidentSpares
    {
        //other properties
        [Required]
        [Display(Name="Spare")]
        [CheckIfSparesAvailable(ErrorMessage="Test")]
        public int SpareID { get; set; }
    }



PS: this is a many-to-many table and am trying to validate the Incident class which contain ICollection of IncidentSpare
here is the Incident model

public class Incidents
    {
        //other properties
        [Display(Name="Spares")]
        public virtual ICollection<IncidentSpares> SpareParts { get; set; }
    }



the create method am talking about is in the incidentController

[update]
am trying to make this work on client side I've written GetClientValidationRules method as follows

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
       {
           var rule = new ModelClientValidationRule
           {
               ValidationType = "checkifsparesavailable",
               ErrorMessage = "Spare is not available in stock",
           };
           yield return rule;
       }



do i have to write javascript to make this work in client side, if yes plz help

解决方案

public class CheckIfSparesAvailableAttribute : ValidationAttribute
    {
        public CheckIfSparesAvailableAttribute() : base() { }
 
        public override bool IsValid(object value)
        {
            int quantity, spareID = (int)value;
 
            quantity = new Spares().CheckQuantity(spareID);
 
            if (quantity < 1){
return new ValidationResult("Please select an option.");
           
}
           else{
 
            return ValidationResult.Success;
}
            
        }
    } 


and since you are using validationattribute, this will hit the controller action method, and in the action method you need to check for the if(ModelState.IsValid)
Because in the custom validation attribute, you are overrriding the IsValid method. You are trying to use the CheckIfSparesAvailableAttribute as an attribute, as AuthorizationAttribute is used. Since in this you are overriding the IsValid method, just you need to return as above and check in the Action for ModelState.IsValid
I hope you get what I tried to explain.
For more info. follow:-
Creating Custom Validation Attribute in MVC 3[^]

Thanks.


这篇关于自定义验证不会阻止创建操作发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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