【问题标题】:ASP.NET Core MVC Model Validation Errormessage not showingASP.NET Core MVC 模型验证错误消息未显示
【发布时间】:2018-09-12 09:46:58
【问题描述】:

我正在尝试验证用户的输入。 验证有效,因为在我的控制器方法中,当某些输入无效时 ModelState.IsValid 为假,当某些输入有效时为真。

我现在遇到的问题是错误消息没有显示给用户。我认为@Html.ValidationMessageFor中的空字符串应该由错误自动填充。这是正确的,对吧? 相同的代码适用于我的注册表单,但在这里不行。

这是我的表单代码:

@using (Html.BeginForm("ChangeProfile", "Account", FormMethod.Post))
{
<div class="form-group">
    <label class="col-lg-3 control-label">Name:</label>
    <div class="col-lg-9">
        @Html.TextBoxFor(n => n.Name, new { @class = "form-control form-control-custom" })
        @Html.ValidationMessageFor(n => n.Name, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">Email:</label>
    <div class="col-lg-9">
        <input disabled class="form-control form-control-custom" type="text" value="@Model.Email" />
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">New Password:</label>
    <div class="col-lg-9">
        @Html.TextBoxFor(n => n.Password, new { type = "password", placeholder = "New Password", @class = "form-control form-control-custom" })
        @Html.ValidationMessageFor(n => n.Password, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">Confirm New Password:</label>
    <div class="col-lg-9">
        @Html.TextBoxFor(n => n.ConfirmPassword, new { type = "password", placeholder = "Confirm New Password", @class = "form-control form-control-custom" })
        @Html.ValidationMessageFor(n => n.ConfirmPassword, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <label class="col-lg-3 control-label">Phone Number:</label>
    <div class="col-lg-9">
        @Html.TextBoxFor(n => n.PhoneNumber, new { @class = "form-control form-control-custom" })
        @Html.ValidationMessageFor(n => n.PhoneNumber, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.ActionLink("DELETE ACCOUNT", "DeleteProfile", Model, new { @class = "btn btn-delete bg-primary" })
    <input type="reset" class="btn btn-default" value="Cancel" style="float: right; margin-right: 15px;">
    <input type="submit" class="btn btn-primary btn-custom" style="margin-right: 10px;" value="Save Changes">
</div>
}

这是我的控制器方法:

public IActionResult ChangeProfile(ProfileViewModel profileViewModel)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction("Profile");
        }
            return View("Overview", accountService.ChangeProfile(profileViewModel));
    }

这是我的 ProfileViewModel:

public class ProfileViewModel
{
    [Required]
    public string Name { get; set; }

    public string Email { get; set; }

    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage = "The passwords do not match.")]
    public string ConfirmPassword { get; set; }

    [Required(ErrorMessage = "The Phone Number field is required.")]
    [Phone(ErrorMessage = "The Phone Number field is not a valid phone number.")]
    public string PhoneNumber { get; set; }
}

【问题讨论】:

  • 如果ModelState 无效,则代码重定向(您需要交换逻辑,以便在ModelState 无效时返回相同的视图)
  • @StephenMuecke 是正确的。如果模型状态无效,我需要重定向。所以不需要交换:)
  • 好吧,这根本没有意义 - 当您重定向时,ModelState 不会被传递。如果ModelState 无效,则需要返回视图,以便显示验证错误。
  • @StephenMuecke 是的,你是对的!当我阅读答案时,我也理解您的评论!谢谢!

标签: c# asp.net-mvc model-validation


【解决方案1】:

这是因为当模型状态无效时,您正在使用 RedirectToAction("Profile") 进行重定向。这样你就失去了“上下文”。

您应该重新渲染 View 。有了这样的东西:

return View("yourView", profileViewModel);

请注意您的“视图模型”不会丢失某些属性(不在您的表单中的属性)。如果是这种情况,您必须重建您的视图模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多