如何在asp.net核心中为伊朗国家代码创建自定义验证属性?

问题描述

我正在尝试在 asp.net core 中验证国家代码。我找到了验证代码,当我将其用作方法时它可以工作,但我想将其创建为验证属性。这是我在属性类中的代码:

public class ValidateNationalCodeAttribute : ValidationAttribute
{        
    protected override ValidationResult IsValid(object value,ValidationContext validationContext)
    {
        string nationalCode = value.ToString();
      
        var isValid = true;

        char[] chArray = nationalCode.ToCharArray();
        int[] numArray = new int[chArray.Length];
        for (int i = 0; i < chArray.Length; i++)
        {
            numArray[i] = (int)char.GetNumericValue(chArray[i]);
        }
        int num2 = numArray[9];
        int num3 = ((((((((numArray[0] * 10) + (numArray[1] * 9)) + (numArray[2] * 8)) + (numArray[3] 
      * 7)) + (numArray[4] * 6)) + (numArray[5] * 5)) + (numArray[6] * 4)) + (numArray[7] * 3)) + 
         (numArray[8] * 2);
        int num4 = num3 - ((num3 / 11) * 11);
        if ((((num4 == 0) && (num2 == num4)) || ((num4 == 1) && (num2 == 1))) || ((num4 > 1) && (num2 
        == Math.Abs((int)(num4 - 11)))))
        {
            isValid = true;
        }
        else
        {
            isValid = false;
        }
        switch (nationalCode)
        {
            case "0000000000":
            case "1111111111":
            case "22222222222":
            case "33333333333":
            case "4444444444":
            case "5555555555":
            case "6666666666":
            case "7777777777":
            case "8888888888":
            case "9999999999":
                isValid = false;
                break;
        }


        if (!isValid)
        {
            return new ValidationResult("invalid");
        }
        return ValidationResult.Success;
    }
}

我就是这么用的。它在一个 viewModel 类中:

    [ValidateNationalCode]
    public string NationalCode { get; set; }

我运行应用程序,例如我输入 1111111111 作为国家代码,但它没有显示任何错误。我该怎么办?

编辑: 这是视图:

 <div class="col-md-4 form-group">
                    <label asp-for="NationalCode" class="required"></label>
                    <input class="form-control text-box single-line valid" 
        asp-for="NationalCode">
                    <span asp-validation-for="NationalCode" class="text- 
       danger"></span>
                </div>

      

这是控制器:

    [HttpPost("{id}")]
    public async Task<IActionResult> installment(long id,NewInstallmentGVM 
        model)
    {           
        if (ModelState.IsValid)
        {
            var Result = await 
         _installmentService.AddInstallmentFromPayment(id,model);
            if (Result.Succeeded)
            {

                return View("InstallmentSucceeded",Result.Value);
            }
            else
            {
                return View("InvalidInstallment");
            }
        }
        return View(model);
    }

解决方法

据我所知,客户端验证与服务器验证不同。我猜您想在客户端显示错误消息。但这不是一回事。

Asp.net core 不支持在客户端显示客户属性验证错误消息。

如果你想这样做,你应该自己写。

更多细节,你可以参考这个article

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...