ASP.NET 核心 MVC 模型验证错误消息未显示
ASP.NET Core MVC Model Validation Errormessage not showing
我正在尝试验证用户的输入。
验证有效,因为在我的控制器方法中,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; }
}
这是因为当模型状态无效时,您正在使用 RedirectToAction("Profile") 进行重定向。通过这样做你失去了 "context" .
您应该重新渲染视图。有了这样的东西:
return View("yourView", profileViewModel);
注意您的 "view model" 没有遗漏某些属性(不在您的表单中的属性)。如果是这种情况,您必须重建视图模型。
我正在尝试验证用户的输入。 验证有效,因为在我的控制器方法中,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; }
}
这是因为当模型状态无效时,您正在使用 RedirectToAction("Profile") 进行重定向。通过这样做你失去了 "context" .
您应该重新渲染视图。有了这样的东西:
return View("yourView", profileViewModel);
注意您的 "view model" 没有遗漏某些属性(不在您的表单中的属性)。如果是这种情况,您必须重建视图模型。