POST前表单模型未验证
本文关键字:验证 模型 表单 POST | 更新日期: 2023-09-27 17:58:10
我正在尝试在我的web应用程序中创建一个注册页面。但是,我的表单上的验证不起作用。例如,我的密码最小长度为4个charter,但如果我输入一个空白密码,它仍然会发出POST请求。我如何告诉表单模型无效,他们应该更改密码、电子邮件等?
AccountController.cs
[HttpPost]
public ActionResult Register(RegisterModel model){
if (ModelState.IsValid){
// Insert into database
}
// There was an error
return View(model);
}
RegisterModel.cs
public class RegisterModel
{
[Required]
[Display(Name = "UserName")]
public string UserName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
Register.cshtml
@model RegisterModel
<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<h4>Create a new account.</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="UserName" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Password" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Register</button>
</div>
</div>
</form>
来源由于Register页面在Home控制器下,而Register逻辑在Account控制器中,因此我必须定义视图来加载Register.cs.html页面。
[HttpPost]
public ActionResult Register(RegisterModel model){
if (ModelState.IsValid){
// Insert into database
}
// There was an error
return View("~/Views/Home/Register.cshtml",model)
}
当您单击提交按钮时,它会将表单发布到服务器,您的操作方法有代码来检查表单是否有效(ModelState.IsValid
)。这是服务器端验证。表单必须发布到服务器才能实现。
如果您希望在客户端进行验证(如果验证失败,则不提交表单),则需要确保已将相关的javascript库/文件加载到页面中。
- jquery.validate.js
- jquery.validate.uno唐突.js
您可以在布局文件或特定视图中包含这些。如果是特定视图,请确保在Scripps部分中包含这些视图。
@section Scripts
{
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation- unobtrusive/jquery.validate.unobtrusive.js"></script>
}
更新文件的路径,使其与项目中这些文件的位置相匹配。您也可以使用公共cdn的位置。