ASP.NET Core MVC模型验证错误消息未显示

时间:2018-09-12 09:46:58

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

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

我现在遇到的问题是错误消息没有显示给用户。我认为@ 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; }
}

1 个答案:

答案 0 :(得分:2)

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

您应该只重新渲染View。像这样的东西:

return View("yourView", profileViewModel);

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