2

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.

enter image description here

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 />
...
nam
  • 21,967
  • 37
  • 158
  • 332
  • Why not take [required] off the property? –  Jun 26 '17 at 17:45
  • @Will When creating a user or even resetting a password you do need to have a user name first. I'm just making it readonly on the `Reset Password UI` so user does not mistype it etc. – nam Jun 26 '17 at 17:55
  • Oh, better then, change the form to remove the name, or just display it in a span? I wouldn't think a password reset would need to do anything else... –  Jun 26 '17 at 18:04
  • @nam In html how are you making textbox read-only? If it is done through `readonly="readonly"` attribute then the value should post back. Also check if any client side validation wired-up on submit click. – Siva Gopal Jun 26 '17 at 18:10
  • @SivaGopal I've just added an UPDATE section to my post. Moreover, the only validation is what `VS2017` creates by default. – nam Jun 26 '17 at 19:08
  • 1
    @Will I tried all of these: a) Removed `name` from Form b) changed input to `span` tag c) changed input tag to `label` tag. But exact same validation error `user name is required`. – nam Jun 26 '17 at 19:28
  • That's a rough one. Good luck! –  Jun 26 '17 at 19:37
  • @Will I agree it's a rough one. Even I tried your first suggestion by taking [required] off in the viewModel and kept input as readonly. But got the error `Value cannot be null. Parameter name: username`. The input tag was displaying the userName correctly - but it seems the readonly attribute of input tag making it's value as null - that in general is not the case as explained [here](https://stackoverflow.com/a/174328/1232087) – nam Jun 26 '17 at 20:03
  • 1
    It really makes no sense to expect `@User.Identity.Name` in the view to bind to `UserName` in the view model. The code that the IDE generated seems wrong. What happens when you change your `asp-for` to = `UserName`? ---> `` – R. Richards Jun 26 '17 at 21:32
  • @R.Richards You're correct (thank you). The issue was using `asp-for=@User.Identity.Name` instead of `asp-for=UserName`. For the benefit of other readers, you may want to write your reasoning in a `Response` and I'll mark it as `Answer`. – nam Jun 27 '17 at 13:58

1 Answers1

0

In this situation, it really makes no sense to expect @User.Identity.Name in the view to bind to UserName in the view model.

It would seem that the code the IDE generated is wrong. Maybe a messed up scaffolding template somewhere, who knows.

You need to change your asp-for to equal UserName. See below.

<input asp-for="UserName" class="form-control" readonly />

Glad this helped!

R. Richards
  • 24,603
  • 10
  • 64
  • 64