Asp.net mvc 模型无效
我正在创建一个注册表。我有一个国家身份号码的财产。但我希望用户在确认注册后填写它。那我就不在登记表上写了 评论:Asp.net mvc 模型无效,asp.net-mvc,model-view-controller,model-validation,Asp.net Mvc,Model View Controller,Model Validation,我正在创建一个注册表。我有一个国家身份号码的财产。但我希望用户在确认注册后填写它。那我就不在登记表上写了 评论: 国家身份号码 @EditorFor(model=>model.IdentityNumber) @Html.ValidationMessageFor(model=>model.IdentityNumber) 这是我的财产: [IdentityNumber("It is not a valid identity number")] [Required(ErrorMessage = "Y
国家身份号码
@EditorFor(model=>model.IdentityNumber)
@Html.ValidationMessageFor(model=>model.IdentityNumber)
这是我的财产:
[IdentityNumber("It is not a valid identity number")]
[Required(ErrorMessage = "You have to enter your national number")]
[DisplayName("National Identity Number:")]
public string IdentityNumber { get; set; }
但这不起作用。我认为原因在于属性[IdentityNumber]
和[Required]
。如果我评论他们
public ActionResult Register(Member model)
{
if (ModelState.IsValid)
{
//.....
ModelState.IsValid
为false
。如果我取消注释它们,它将返回true。那么,我必须在哪里更改某些内容以允许它?我的意思是,我希望用户在注册后写下他/她的身份号码
在my db中,标识国家编号字段也允许null
编辑:这是我的属性代码:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false, Inherited = true)]
public class IdentityNumberAttribute : ValidationAttribute, IClientValidatable
{
private string WrongIdentityNumber;
public IdentityNumberAttribute(string message)
:base("Invalid an identity number")
{
WrongIdentityNumber = message;
}
private string identityNumber;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
return new ValidationResult(WrongIdentityNumber);
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);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule validationRule = new ModelClientValidationRule();
validationRule.ValidationType = "identitynumber";
validationRule.ErrorMessage = "Invalid identity number";
validationRule.ValidationParameters.Add("param", "");
return new List<ModelClientValidationRule> { validationRule };
//var rule = new ModelClientValidationRule();
//rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
//rule.ValidationParameters.Add("identitynumber", identityNumber); //küçük harfle yaz html kuralı
//rule.ValidationType = "identitynumber";
//yield return rule;
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple=false,Inherited=true)]
公共类标识Number属性:ValidationAttribute,IClientValidable
{
私有字符串错误标识符编号;
公共标识编号属性(字符串消息)
:base(“标识号无效”)
{
ErrorIdentityNumber=消息;
}
私有字符串标识符编号;
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
如果(值==null)
返回新的ValidationResult(错误标识编号);
identityNumber=value.ToString();
if(identityNumber.Length!=11)
返回新的ValidationResult(错误标识编号);
整数和=0;
对于(int i=0;i
您可以添加此行
ModelState.Remove("ErrorKey");//you can find the error key by stepping though your code
if (ModelState.IsValid)
{
.....
“ErrorKey”类似于“YourModelName.YourPropertyName”。设置断点并将鼠标悬停在ModelState
上,您可以看到所有键
但是,您应该创建一个只具有所需属性的视图模型,将数据注释放在其中,而不包含域模型。您可以添加这一行
ModelState.Remove("ErrorKey");//you can find the error key by stepping though your code
if (ModelState.IsValid)
{
.....
“ErrorKey”类似于“YourModelName.YourPropertyName”。设置断点并将鼠标悬停在ModelState
上,您可以看到所有键
但是,您应该创建一个仅具有所需属性的视图模型,将数据注释放在其中,并将域模型排除在外。您已经设置了[Required]属性。删除此项,然后尝试我尝试过的。如前所述,IdentityNumber属性也是一个问题,因为您已经设置了[Required]属性。删除此项,然后尝试我尝试过的。正如我前面所写的,IdentityNumber属性也是一个问题。通常我在注册表中需要它,但我们决定更改它。然后,在用户完成注册后,我们将需要它。:)通常我需要在登记表中填写,但我们决定更改它。然后,在用户完成注册后,我们将需要它。:)