As we know when you create an ASP.NET Core appp using Individual User Authentication
project template, it creates a default ResetPassword.cshtml
view. In that View
I need to set logged in user name input tag as readonly
. But doing so is throwing the following validation error. If I don't make it readonly
the below screen successfully allows user to change password.
Question: Why the following validation error on form submit - when the UserName input tag is set to readonly
? I know that if the input tag is disabled then form submit does not submit the input tag's value (also explained by @AdamBellaire
here). It seems [Required]
annotation in public string UserName { get; set; }
is somehow conflicting with readonly attribute of input tag.
public class ResetPasswordViewModel
{
[Required]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string Code { get; set; }
}
UPDATE
ResetPassword.cshtml
: [It's a default View created by VS2017. Only change is that I added readonly
attribute in input tag below]
@model ResetPasswordViewModel
...
<input asp-for="@User.Identity.Name" class="form-control" readonly />
...