retain the Password Text box values after submit in MVC

混江龙づ霸主 提交于 2020-01-02 10:04:54

问题


I am working in an MVC Application. In our Application. We have a Form which getting the Details for a user (client Details). Example, mild, First name, Last name, Password etc.. After Submit, in the Controller itself, we verify whether the entered MailID had already been registered or not. If already registered means, Return the same view with an error message. But all details are present in the Textbox, but the Password Textbox becomes empty. I want the Password text box with the value Which is Entered already.

public string Email { get; set; }

    [Required(ErrorMessage = "FirstName is required")]
    public string FirstName { get; set; }

    public string LastName { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password)]
    [StringLength(40, MinimumLength = 6,
    ErrorMessage = "Password must be between 6 and 12 characters.")]
    public string Password { get; set; }

回答1:


That's by design of the Html.Password and Html.PasswordFor helpers and unfortunately it cannot be changed because it is hardcoded in the helper itself:

public static MvcHtmlString PasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
{
    if (expression == null)
    {
        throw new ArgumentNullException("expression");
    }
    object obj2 = null;
    IDictionary<string, object> dictionary = htmlAttributes;
    return PasswordHelper(htmlHelper, ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData), ExpressionHelper.GetExpressionText(expression), obj2, dictionary);
}

See this object obj2 = null; over there?

If you want to change this behavior you could use the TextBoxFor helper to generate your password fields:

@Html.TextBoxFor(x => x.Password, new { type = "password" })


来源:https://stackoverflow.com/questions/21794016/retain-the-password-text-box-values-after-submit-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!