【发布时间】:2021-04-30 00:17:27
【问题描述】:
在我的模型 RegisterViewModel on Password 属性中,我做了两个验证属性。在这种情况下,我使用通常的 MVC 和 jQuery 验证。问题是验证消息显示唯一的
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{0,}$", ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]
如果密码不在 6-15 个字符之间,我希望显示来自该属性的消息
[StringLength(15, ErrorMessage = "The password must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
密码属性:
[Required(ErrorMessage = "Password field is required")]
[StringLength(15, ErrorMessage = "The password must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{6,15}$",ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
这是视图:
@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
@Html.AntiForgeryToken();
<div class="form-horizontal">
<h4>Contacts</h4>
<hr />
@Html.HiddenFor(x => Model.RegisterViewModel.OrganizationId)
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.Email, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.Email, htmlAttributes: new { @class = "form-control", @readonly="readonly" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.FirstName, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.FirstName, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.LastName, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.LastName, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.Mobile, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.Mobile, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.Mobile, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.Role, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.DropDownListFor(x => Model.RegisterViewModel.Role, new SelectList(Model.RegisterViewModel.Roles, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "Role" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.Role, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.Department, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.Department, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.Department, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.Password, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.Password, htmlAttributes: new { @class = "form-control", placeholder="Password", @type = "password" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.RegisterViewModel.ConfirmPassword, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.RegisterViewModel.ConfirmPassword, htmlAttributes: new { @class = "form-control", placeholder="Confirm Password", @type = "password" })
@Html.ValidationMessageFor(x => x.RegisterViewModel.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
</div>
<br />
}
【问题讨论】:
-
属性列表看起来正确。它是 ASP.NET MVC 5 吗?请把视图的相关部分暴露一下好吗?
标签: c# asp.net-mvc razor unobtrusive-validation