向视图显示 ModelState 错误

Show ModelState errors to te View

我正在尝试向用户显示一些错误。

为此,我正在使用 ModelState.isValid@Html.ValidationSummary 来显示它。

但不知何故,它目前不起作用。

型号:

[Required(ErrorMessage = "Account name is required")]
[EmailAddress]
public string CTC_accountName { get; set; }

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

控制器:

[HttpPost]
public async Task<IActionResult> Submit(RegisterViewModel model)
{
        if (!ModelState.IsValid)
        {
          return View(model);
        }
    }
}

查看:

@using (Html.BeginForm("Submit", "Register", FormMethod.Post, new { id = "formAll", style = "margin-top:3%", enctype = "multipart/form-data" }))
{

    <div class="shadow-sm p-2 mb-3 rounded">
        <div class="col-sm-9">
            <h5>Personal Details</h5>
        </div>
    </div>
    <div id="formMember">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group row">
            <div class="col-sm-2">
                @Html.LabelFor(model => model.CTC_accountName, "Account/Email*")
            </div>
            <div class="col-sm-10">
                @Html.EditorFor(model => model.CTC_accountName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @id = "CTC_accountName", @autocomplete = "username" } })
            </div>
            <div class="col-sm-10">
                 @Html.ValidationMessageFor(model => model.CTC_accountName)
            </div>
        </div>
        <div class="form-group row">
            <div class="col-sm-2">
                @Html.LabelFor(model => model.CTC_accountPassword, "Password*")
            </div>
            <div class="col-sm-10">
                @Html.EditorFor(model => model.CTC_accountPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Password", @id = "CTC_accountPassword", @type = "password", @autocomplete = "current-password" } })
            </div>
            <div class="col-sm-10">
                @Html.ValidationMessageFor(model => model.CTC_accountName)
            </div>
        </div>
    </div>
}

感谢您的帮助

我发现问题所在了。

我在提交函数中有一个 POST 调用 ajax 我必须删除才能看到 @Html.ValidationSummary!