本文介绍了串并MINLENGTH验证MAXLENGTH不工作(asp.net MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类

 使用System.ComponentModel.DataAnnotations;
使用Argussoft.BI.DAL.Domain.Users;命名空间Argussoft.BI.DAL.DTOs.UserDTOs
{
公共类CreateUserDto
{
    [必需(的ErrorMessage =Введителогин)]
    [MAXLENGTH(User.EmailLength,的ErrorMessage =Максимальнаядлиналогина40символов)]
    [RegularEx pression(User.NameRegularEx pression,的ErrorMessage =Логинможетсодержатьтольколатинскиесимволы,дефисы,подчеркивания,точки)]
    公共字符串名称{;组; }    [必需(的ErrorMessage =Введите电子邮件)]
    [MAXLENGTH(User.EmailLength,的ErrorMessage =Максимальнаядлинаадресаэлектроннойпочты100символов)]
    [RegularEx pression(User.EmailRegularEx pression,的ErrorMessage =Введитекорректныйадресэлектроннойпочты)]
    公共虚拟字符串电子邮件{获得;组; }    [必需(的ErrorMessage =Введитеимяпользователя)]
    [MAXLENGTH(User.FullNameLength,的ErrorMessage =Максимальнаядлинаименипользователя100символов)]
    [RegularEx pression(User.NameRegularEx pression,的ErrorMessage =Имяпользователяможетсодержатьтольколатинскиесимволы,дефисы,подчеркивания,точки)]
    公共虚拟字符串全名{获得;组; }    公共虚拟INT角色{搞定;组; }    [RegularEx pression(User.PhoneRegularEx pression,的ErrorMessage =Введитекорректныйномертелефона)]
    公共虚拟字符串电话{搞定;组; }    公共虚拟INT状态{搞定;组; }    [必需(的ErrorMessage =Введитепароль)]
    [MINLENGTH个(User.PasswordMinLength,的ErrorMessage =Минимальнаядлинапароля5символов)]
    [MAXLENGTH(User.PasswordMaxLength,的ErrorMessage =Максимальнаядлинапароля20символов)]
    [RegularEx pression(User.PasswordRegularEx pression,的ErrorMessage =Парольможетсодержатьтольколатинскиесимволы,дефисы,подчеркивания,точки)]
    公共虚拟字符串密码{搞定;组; }
}
}

这种形式

  @model Argussoft.BI.DAL.DTOs.UserDTOs.CreateUserDto@using(Ajax.BeginForm(CREATEUSER,用户,新AjaxOptions {=的onSuccessonSuccessCreateUser},{新的id =dialog_form,@class =形横}))
{
    < D​​IV CLASS =模头>
        <按钮式=按钮级=关闭数据解雇=莫代尔ARIA隐藏=真>&安培;倍;< /按钮>
    < H4>Добавитьпользователя< / H4>
< / DIV>
< D​​IV CLASS =模体>    <! - 姓名 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Name)>Логин< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.TextBoxFor(型号=> model.Name,新{@class =span3ID =input_name})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Name)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 电子邮件 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Email)>电子邮件和LT; /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.TextBoxFor(型号=> model.Email,新{@class =span3ID =input_email})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Email)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 全名 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.FullName)>Имяпользователя< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.TextBoxFor(型号=> model.FullName,新{@class =span3ID =input_full_name})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.FullName)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 角色 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Role)>Рольпользователя< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.DropDownList(角色,(的SelectList)ViewBag.Roles,新{ID =input_role})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Role)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 电话 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Phone)>Контактныйтелефон< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.TextBoxFor(型号=> model.Phone,新{@class =span3ID =input_phone})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Phone)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 状态 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Status)>Статуспользователя< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.DropDownList(状态,(的SelectList)ViewBag.UserStatuses,新{ID =input_status})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Status)
            < / DIV>
        < / DIV>
    < / DIV>    <! - 密码 - >
    < D​​IV CLASS =控制组>
        <标签类=控制标签为=@ Html.NameFor(型号=> model.Password)>Парольпользователя< /标签>
        < D​​IV CLASS =控制>
            < D​​IV CLASS =输入 - prePEND>
                @ Html.TextBoxFor(型号=> model.Password,新{@class =span3ID =input_password})
                < BR />
                @ Html.ValidationMessageFor(型号=> model.Password)
            < / DIV>
        < / DIV>
    < / DIV>< / DIV>
< D​​IV CLASS =模式躯>
    < A HREF =@ Url.Action(UserIndex,用户)级=BTN>Отменить< / A>
    <按钮式=提交ID =save_button级=BTN BTN-小学>Сохранить< /按钮>
< / DIV>
}

但我的验证只能所需和 RegularEx pression MINLENGTH个不工作, 最大长度我不明白在这种情况下任何错误消息。

什么可能是一个原因?


解决方案

MaxLength用于实体框架来决定如何大,以使一个字符串值字段时,它会创建数据库。

从MSDN:

StringLength is a data annotation that will be used for validation of user input.

From MSDN:

Non Customized

Use [String Length]

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

Customized StringLengthAttribute Class

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

这篇关于串并MINLENGTH验证MAXLENGTH不工作(asp.net MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:45