【发布时间】:2012-12-16 17:13:29
【问题描述】:
标题看起来有点奇怪,抱歉。
我是 Asp.net MVC 3 的新手。我想为我的一个属性创建一个属性,该属性的名称是国家身份证号码。
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 的值。
我希望我能解释一下我的问题
【问题讨论】:
-
好的,我找到了解决方案。我已经实现了 ValidationAttribute 而不是 ActionFilterAttribute :)
-
如果您找到了您的解决方案,请将其作为答案发布并接受。这样未来的用户就会了解如何解决问题。
标签: c# asp.net-mvc-3 attributes actionfilterattribute