【发布时间】:2017-04-26 17:10:31
【问题描述】:
我有一个 .NET Core 应用程序,一旦从 get 返回页面,就会触发验证。我有一个视图模型,但我不确定为什么收到响应后就已经触发了验证。
这是我的模型和标记
using System.ComponentModel.DataAnnotations;
namespace MusicianProject.Models.ViewModels
{
public class RegisterViewModel
{
[Required(ErrorMessage ="First name is required.")]
[Display(Name ="First Name")]
[StringLength(25, ErrorMessage = "First name must be less than 25 characters.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name is required")]
[Display(Name = "Last Name")]
[StringLength(50, ErrorMessage = "Last name must be less than 50 characters.")]
public string LastName { 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 = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}
这是返回的寄存器视图。
@model MusicianProject.Models.ViewModels.RegisterViewModel
<div class="container">
<form asp-controller="Account" asp-action="Register" method="post">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label asp-for="FirstName"></label>
<input class="form-control" type="text" asp-for="FirstName" />
<span asp-validation-for="FirstName">First name is required.</span>
</div>
<div class="form-group">
<label asp-for="LastName"></label>
<input class="form-control" asp-for="LastName" />
<span asp-validation-for="LastName">Last name is required.</span>
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input class="form-control" type="text" asp-for="Email" />
<span asp-validation-for="Email">Email is required.</span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" type="password" id="password" class="form-control" />
<span asp-validation-for="Password">Password is required.</span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" type="password" id="confirm-password" class="form-control" />
<span asp-validation-for="ConfirmPassword">Confirm password is required</span>
</div>
<div class="btn-group text-center">
<button class="btn btn-default">Sign up!</button>
<button class="btn btn-danger">Cancel</button>
</div>
</div>
</form>
</div>
我有两种注册方法,post 和 get。这是我所拥有的。
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Register(RegisterViewModel rvm)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index", "Home");
}
return View();
}
当我在浏览器中导航到路线时,这就是我得到的。
【问题讨论】:
标签: asp.net validation asp.net-core asp.net-core-mvc