【发布时间】: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