简体   繁体   English

为什么我的表单验证器不验证我的表单?

[英]Why isn't my form validator validating my form?

So I'm trying to create a basic register page where the user needs to fill out all the fields in order to proceed.所以我正在尝试创建一个基本的注册页面,用户需要填写所有字段才能继续。 But the issue I'm facing is that when I click "Create" to finish the process, it just refreshes the page and there are no errors shown, even if I don't fill out any of the fields.但我面临的问题是,当我单击“创建”完成该过程时,它只是刷新页面并且没有显示任何错误,即使我没有填写任何字段。

This is the Controller这是 Controller

public IActionResult Register()
{
    return View();
}

This is the form这是表格

<form>
    <div asp-validation-summary="All" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="Username" class="control-label"></label>
        <input asp-for="Username" class="form-control" />
        <span asp-validation-for="Username" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Password" class="control-label"></label>
        <input asp-for="Password" class="form-control" />
        <span asp-validation-for="Password" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="ConfirmPassword" class="control-label"></label>
        <input asp-for="ConfirmPassword" class="form-control" />
        <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
    </div>
    <div class="form-group form-check">
        <label class="form-check-label">
            <input class="form-check-input" asp-for="Agree" /> @Html.DisplayNameFor(model => model.Agree)
        </label>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

And obviously the model显然 model

public class RegisterAccountModel
{
    [Required(ErrorMessage = "This field is required.")]
    [StringLength(60, MinimumLength = 3)]
    [DataType(DataType.Text)]
    public string Username { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage = "The Password and Confirm password must match.")]
    public string ConfirmPassword { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    //[DataType(DataType.Password)]
    public bool Agree { get; set; }
}

Why is the page just refreshing without showing any errors when I click the "Create" button?为什么单击“创建”按钮时页面只是刷新而没有显示任何错误?

You need to use the model state valid to start the valuation您需要使用 model state 有效才能开始估价

public IActionResult Register()
{

    if (ModelState.IsValid) { //checking model state
                // send to the original action 
                return RedirectToAction("Index");
    }
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM