Hei*_*del 47 c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
我有这门课
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
和这种形式
@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">×</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>
}
Run Code Online (Sandbox Code Playgroud)
用户类:
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; //...
}
Run Code Online (Sandbox Code Playgroud)
但我的验证仅适用于Required和RegularExpression不起作用,MinLength并且MaxLength在这种情况下我没有收到任何错误消息.
可能是什么原因?
Ima*_*ani 119
MaxLength用于实体框架,以决定在创建数据库时创建字符串值字段的大小.
来自MSDN:
指定属性中允许的数组或字符串数据的最大长度.
StringLength是一个数据注释,将用于验证用户输入.
来自MSDN:
指定数据字段中允许的最小和最大字符长度.
使用 [String Length]
[RegularExpression(@"^.{5,}$", ErrorMessage = "Minimum 3 characters required")]
[Required(ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "Invalid")]
Run Code Online (Sandbox Code Playgroud)
30是最大长度
最小长度= 3
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; }
}
Run Code Online (Sandbox Code Playgroud)
尝试使用此属性,例如密码最小长度:
[StringLength(100, ErrorMessage = "???????????? ????? ?????? 20 ????????", MinimumLength = User.PasswordMinLength)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
123171 次 |
| 最近记录: |