Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从actionfilter中的属性获取值_C#_Asp.net Mvc 3_Attributes_Actionfilterattribute - Fatal编程技术网

C# 如何从actionfilter中的属性获取值

C# 如何从actionfilter中的属性获取值,c#,asp.net-mvc-3,attributes,actionfilterattribute,C#,Asp.net Mvc 3,Attributes,Actionfilterattribute,这个标题看起来有点奇怪,抱歉 我是Asp.NETMVC3的新手。我想为我的一个属性创建一个属性,该属性的名称是national identity number public class IdentityNumberControl : ActionFilterAttribute { public string WrongIdentityNumber { get; set; } public override void OnActionExecuting(ActionExec

这个标题看起来有点奇怪,抱歉

我是Asp.NETMVC3的新手。我想为我的一个属性创建一个属性,该属性的名称是national identity number

    public class IdentityNumberControl : ActionFilterAttribute
{
    public string WrongIdentityNumber { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        if(??? )
        {
            filterContext.HttpContext.Response.Write(WrongIdentityNumber);
            return;
        }
        base.OnActionExecuting(filterContext);
    }
}
我的会员班在这里

 public class Member
{
    public int ID { get; set; }
    [Required(ErrorMessage = "You have to enter your name")]
    [StringLength(50, ErrorMessage  ="Your name length can not be less than{2} more than {1} ", MinimumLength = 3)]
    [Display("Name :")]
    public string Name { get; set; }

    [Required(ErrorMessage = "You have to enter your surname")]
    [StringLength(40, ErrorMessage = "Your surname length can not be less than{2} more than {1} ", MinimumLength = 2)]
    [Display("Surname :")]
    public string SurName { get; set; }

    [Required(ErrorMessage = "You have to enter your password")]
    [StringLength(20, ErrorMessage = "Your name lengt can not be less than{2} more than {1} ", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display("Password :")]
    public string Password { get; set; }
    public string PasswordSalt { get; set; }

    [IdentityNumberControl(WrongIdentityNumber = "It is not a valid identity number")]
    public double IdentityNumber { get; set; }

}
所以我想检查identitynumber是否为真(我有一个计算方法来确认) 我知道我必须在IdentityNumberControl类中编写代码。但我不知道如何在OnActionExecuting方法中获得identitynumber的值

我希望我能解释我的问题这里是我的解决方案

public class IdentityNumberAttribute : ValidationAttribute
{
    private string WrongIdentityNumber;

    public IdentityNumberAttribute(string message)
    {
        WrongIdentityNumber = message;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        string identityNumber = value.ToString();

        if (identityNumber.Length != 11)
            return new ValidationResult(WrongIdentityNumber);

        int sum = 0;


        for (int i = 0; i < identityNumber.Length - 1; i++)
        {
            sum += Convert.ToInt32(identityNumber[i].ToString());
        }

        return sum.ToString()[1] == identityNumber[10]
                   ? ValidationResult.Success
                   : new ValidationResult(WrongIdentityNumber);
    }
}

好吧,我找到了解决办法。我已经实现了ValidationAttribute而不是ActionFilterAttribute:)如果您找到了解决方案,请将其作为答案发布并接受。这样,未来的用户就会了解如何解决这个问题。
    [IdentityNumber("It is not a valid identity number")]
    [Required(ErrorMessage = "You have to enter your identity number")]
    [DisplayName("National Identity Number:")]
    public string IdentityNumber { get; set; }