60

我有这门课

using System.ComponentModel.DataAnnotations;
using Argussoft.BI.DAL.Domain.Users;

namespace Argussoft.BI.DAL.DTOs.UserDTOs
{
    public class CreateUserDto
    {
        [Required(ErrorMessage = "Введите логин")]
        [MaxLength(User.EmailLength, ErrorMessage = "Максимальная длина логина 40 символов")]
        [RegularExpression(User.NameRegularExpression, ErrorMessage = "Логин может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Введите Email")]
        [MaxLength(User.EmailLength, ErrorMessage = "Максимальная длина адреса электронной почты 100 символов")]
        [RegularExpression(User.EmailRegularExpression, ErrorMessage = "Введите корректный адрес электронной почты")]
        public virtual string Email { get; set; }

        [Required(ErrorMessage = "Введите имя пользователя")]
        [MaxLength(User.FullNameLength, ErrorMessage = "Максимальная длина имени пользователя 100 символов")]
        [RegularExpression(User.NameRegularExpression, ErrorMessage = "Имя пользователя может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public virtual string FullName { get; set; }

        public virtual int Role { get; set; }

        [RegularExpression(User.PhoneRegularExpression, ErrorMessage = "Введите корректный номер телефона")]
        public virtual string Phone { get; set; }

        public virtual int Status { get; set; }

        [Required(ErrorMessage = "Введите пароль")]
        [MinLength(User.PasswordMinLength, ErrorMessage = "Минимальная длина пароля 5 символов")]
        [MaxLength(User.PasswordMaxLength, ErrorMessage = "Максимальная длина пароля 20 символов")]
        [RegularExpression(User.PasswordRegularExpression, ErrorMessage = "Пароль может содержать только латинские символы, дефисы, подчеркивания, точки")]
        public virtual string Password { get; set; }
    }
}

和这个表格

@model Argussoft.BI.DAL.DTOs.UserDTOs.CreateUserDto

@using (Ajax.BeginForm("CreateUser", "User", new AjaxOptions { OnSuccess = "onSuccessCreateUser" }, new { id = "dialog_form", @class = "form-horizontal" }))
{
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4>Добавить пользователя</h4>
    </div>
    <div class="modal-body">

        <!-- Name -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Name)">Логин</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Name, new { @class = "span3", id = "input_name" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>
        </div>

        <!-- Email -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Email)">Email</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Email, new { @class = "span3", id = "input_email" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Email)
                </div>
            </div>
        </div>

        <!-- FullName -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.FullName)">Имя пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.FullName, new { @class = "span3", id = "input_full_name" })
                    <br />
                    @Html.ValidationMessageFor(model => model.FullName)
                </div>
            </div>
        </div>

        <!-- Role -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Role)">Роль пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.DropDownList("Role", (SelectList)ViewBag.Roles,new{id ="input_role"})
                    <br />
                    @Html.ValidationMessageFor(model => model.Role)
                </div>
            </div>
        </div>

        <!-- Phone -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Phone)">Контактный телефон</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Phone, new { @class = "span3", id = "input_phone" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Phone)
                </div>
            </div>
        </div>

        <!-- Status -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Status)">Статус пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.DropDownList("Status", (SelectList)ViewBag.UserStatuses,new{id ="input_status"})
                    <br />
                    @Html.ValidationMessageFor(model => model.Status)
                </div>
            </div>
        </div>

        <!-- Password -->
        <div class="control-group">
            <label class="control-label" for="@Html.NameFor(model => model.Password)">Пароль пользователя</label>
            <div class="controls">
                <div class="input-prepend">
                    @Html.TextBoxFor(model => model.Password, new { @class = "span3", id = "input_password" })
                    <br />
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>
        </div>

    </div>
    <div class="modal-footer">
        <a href="@Url.Action("UserIndex", "User")" class="btn">Отменить</a>
        <button type="submit" id="save_button" class="btn btn-primary">Сохранить</button>
    </div>
}

用户等级:

public class User : BaseEntity 
{ 
    public const int NameLength = 40; 
    public const int PasswordMinLength = 5; 
    public const int PasswordMaxLength = 20; 
    public const int FullNameLength = 100; 
    public const int EmailLength = 100; 
    public const int PhoneLength = 40; //...
}

但我的验证仅适用于RequiredandRegularExpression不适用于MinLengthandMaxLength在这种情况下我没有收到任何错误消息。

可能是什么原因?

4

5 回答 5

139

MaxLength用于实体框架在创建数据库时确定字符串值字段的大小。

来自 MSDN:

指定属性中允许的数组或字符串数​​据的最大长度。

StringLength是一个数据注释,将用于验证用户输入。

来自 MSDN:

指定数据字段中允许的最小和最大字符长度。

非定制

采用[String Length]

[RegularExpression(@"^.{3,}$", ErrorMessage = "Minimum 3 characters required")]
[Required(ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "Maximum 30 characters")]

30 是最大长度
最小长度 = 3

自定义 StringLengthAttribute 类

public class MyStringLengthAttribute : StringLengthAttribute
{
    public MyStringLengthAttribute(int maximumLength)
        : base(maximumLength)
    {
    }

    public override bool IsValid(object value)
    {
        string val = Convert.ToString(value);
        if (val.Length < base.MinimumLength)
            base.ErrorMessage = "Minimum length should be 3";
        if (val.Length > base.MaximumLength)
            base.ErrorMessage = "Maximum length should be 6";
        return base.IsValid(value);
    }
}

public class MyViewModel
{
    [MyStringLength(6, MinimumLength = 3)]
    public String MyProperty { get; set; }
}
于 2013-08-16T15:28:15.477 回答
9

尝试使用此属性,例如密码最小长度:

[StringLength(100, ErrorMessage = "Максимальная длина пароля 20 символов", MinimumLength = User.PasswordMinLength)]
于 2013-08-16T15:27:52.453 回答
4

这可以替换 MaxLength 和 MinLength

[StringLength(40, MinimumLength = 10 , ErrorMessage = "Password cannot be longer than 40 characters and less than 10 characters")]
于 2016-11-19T03:35:31.063 回答
3

他们现在使用最新版本的 MVC(和 jquery 验证包)。mvc51-release-notes#Unobtrusive

感谢这个答案指出来!

于 2014-06-26T03:42:49.253 回答
3
    [StringLength(16, ErrorMessageResourceName= "PasswordMustBeBetweenMinAndMaxCharacters", ErrorMessageResourceType = typeof(Resources.Resource), MinimumLength = 6)]
    [Display(Name = "Password", ResourceType = typeof(Resources.Resource))]
    public string Password { get; set; }

像这样保存资源

"ThePasswordMustBeAtLeastCharactersLong" | "The password must be {1} at least {2} characters long"
于 2015-12-12T10:33:28.730 回答