Html.PasswordFor is not populated?

2019-05-09 15:44发布

问题:

I spend a lot of my time, trying to understand why in a razor expression of

@HTML.PasswordFor( m => m.Password)

I can't set the value from the model the only solution that I found it's injecting the value by html properties like this

@HTML.PasswordFor( m => m.Password, new { value = Model.Password })

Am I doing something wrong?? is the correct helper?? this is the field model configuration

[Required(ErrorMessage = "La contraseña es obligatoria.")]
[StringLength(100, ErrorMessage = "El {0} debe tener al menos {2} caracteres de longitud.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Contraseña")]
public string Password { get; set; }

It's the only solution?

回答1:

It's a security feature. To make this work implies that a) you are able to access the users password in plain text and b) are transmitting it over the wire to their browser.

Neither of which are safe and shouldn't be encouraged.

As such, this is a feature of Razor, rather than an issue.