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

标签 asp.net-core attributes validationattribute

我正在尝试验证 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 作为国家代码,但它没有显示任何错误。我该怎么办?

编辑: 这是 View :

 <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>

      

这是 Controller :

    [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 .

关于asp.net-core - 如何在asp.net core中为伊朗国家代码创建自定义验证属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67396158/

相关文章:

c# - Http 错误 404.13 ASP.NET Core MVC 的自定义错误页面

c# - 如何在 ASP.NET MVC 6 中继续使用 ModelState 和 RedirectToAction?

asp.net-mvc - 用于客户端身份验证的 OpenIdConnect HTTP 基本身份验证方案

JavaScript 对象,两个问题

javascript - 为什么在svg中设置xlink href时setAttribute/getAttribute与参数不一致?

asp.net-core - Blazor HttpClient 注入(inject)到 ViewModel 构造函数中

c# - 属性类如何工作?

c# - DateTime 的 MVC3 ValidationAttribute?在 IsValid(...) 中返回 DateTime.Max

regex - 客户端验证不支持使用 Unicode 的 ASP.NET MVC 正则表达式验证

C# 无法使用自定义验证属性验证属性