Я пытаюсь подтвердить ввод пользователя.Проверка работает, потому что в моем методе контроллера 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; }
}