Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
Asp.net mvc 自定义的StringLength验证不起作用_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 自定义的StringLength验证不起作用

Asp.net mvc 自定义的StringLength验证不起作用,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我在项目中添加了自定义的StringLength类 using System; using System.ComponentModel.DataAnnotations; namespace Argussoft.BI.DAL.Validation { public class PasswordLengthAttribute : StringLengthAttribute { public PasswordLengthAttribute(int maximumLength)

我在项目中添加了自定义的StringLength类

using System;
using System.ComponentModel.DataAnnotations;

namespace Argussoft.BI.DAL.Validation
{
public class PasswordLengthAttribute : StringLengthAttribute
{
    public PasswordLengthAttribute(int maximumLength)
        : base(maximumLength)
    {
    }

    public override bool IsValid(object value)
    {
        string val = Convert.ToString(value);
        if (val.Length < MinimumLength)
            ErrorMessage = "Минимальная длина пароля 5 символов";
        if (val.Length > MaximumLength)
            ErrorMessage = "Максимальная длина пароля 20 символов";
        return base.IsValid(value);
    }
}
}
这是我的控制器

[HttpPost]
    public ActionResult CreateUser(CreateUserDto dto)
    {
        if (!Request.IsAjaxRequest()) return View("_AddUserDialog", dto);
        if (!ModelState.IsValid) {FillViewBags(); return PartialView("_AddUserDialog", dto);}
        string hash;
        string salt;
        CryptoUtils.SetHashAndSalt(dto.Password, out hash, out salt);
        UserService.CreateUser(
            new User ()
            {
                Name = dto.Name,
                Email = dto.Email,
                FullName = dto.FullName,
                Role = UserService.SetRole(dto.Role),
                Phone = dto.Phone,
                Status = UserService.SetUserStatus(dto.Status),
                PasswordHash = hash,
                PasswordSalt = salt
            });
        return Json(new { Message = string.Format("Пользователь {0} успешно создан", dto.Name) });
    }
但是如果我尝试输入的密码少于5个字符或超过20个字符,ModelState.IsValid==false,但是
当密码少于5个字符或超过20个字符时,我不会收到任何错误消息。有什么问题?

如果您将错误消息放在装饰上,就像其他两个属性上一样?您是否尝试使用常量值而不是User.PasswordMaxLength?@PKKG是的,我尝试使用常量,但是[PasswordLength5,MinimumLength=20]不会给我带来错误消息。您是否设置了调试器,在StringLengthatAttribute类中。我试图在调试器中检查代码,StringLengthAttribute类可以工作,但它看起来像是return base.IsValidvalue;不会返回true。
[HttpPost]
    public ActionResult CreateUser(CreateUserDto dto)
    {
        if (!Request.IsAjaxRequest()) return View("_AddUserDialog", dto);
        if (!ModelState.IsValid) {FillViewBags(); return PartialView("_AddUserDialog", dto);}
        string hash;
        string salt;
        CryptoUtils.SetHashAndSalt(dto.Password, out hash, out salt);
        UserService.CreateUser(
            new User ()
            {
                Name = dto.Name,
                Email = dto.Email,
                FullName = dto.FullName,
                Role = UserService.SetRole(dto.Role),
                Phone = dto.Phone,
                Status = UserService.SetUserStatus(dto.Status),
                PasswordHash = hash,
                PasswordSalt = salt
            });
        return Json(new { Message = string.Format("Пользователь {0} успешно создан", dto.Name) });
    }