在MVC中提交后保留密码文本框值 [英] retain the Password Text box values after submit in MVC

查看:82
本文介绍了在MVC中提交后保留密码文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

这是Html.PasswordHtml.PasswordFor帮助器的设计,不幸的是,由于它是硬编码在帮助器本身中的,因此无法更改:

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

在那看到这个object obj2 = null;吗?

如果要更改此行为,可以使用TextBoxFor帮助器来生成密码字段:

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

这篇关于在MVC中提交后保留密码文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆