【问题标题】:retain the Password Text box values after submit in MVC在 MVC 中提交后保留密码文本框的值
【发布时间】:2014-03-14 16:18:09
【问题描述】:

我正在使用 MVC 应用程序。在我们的应用程序中。我们有一个获取用户详细信息的表单(客户端详细信息)。例如,轻度、名字、姓氏、密码等。 提交后,在Controller本身中,我们验证输入的MailID是否已经注册。如果已经注册,则返回相同的视图并显示错误消息。但是所有详细信息都存在于文本框中,但密码文本框变为空。我想要已经输入值的密码文本框。

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; }

【问题讨论】:

    标签: asp.net-mvc passwords


    【解决方案1】:

    这是 Html.PasswordHtml.PasswordFor 助手的设计,不幸的是它无法更改,因为它是在助手本身中硬编码的:

    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);
    }
    

    在那边看到object obj2 = null;了吗?

    如果您想更改此行为,您可以使用 TextBoxFor 帮助程序来生成您的密码字段:

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

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2014-10-29
      • 2019-03-13
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      相关资源
      最近更新 更多