【问题标题】:Create Password field @html.TextBoxFor创建密码字段@html.TextBoxFor
【发布时间】:2015-03-29 13:59:39
【问题描述】:

模型类

[Required(ErrorMessage = "Pass word is required")]
//[ValidatePasswordLength]
[DataType(DataType.Password)]
//[Display(Name = "Password")]
public string Password { get; set; }

查看

<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Password, new {@class="form-control",placeholder="Password" })
@Html.ValidationMessageFor(model => model.Password)

</div>

我用谷歌搜索了它。我发现使用 @html.Editor 而不是 @html.TextBoxFor 但我必须为文本框应用 css。

我使用了@Html.TextBoxFor(model =&gt; model.Password, new {@class="form-control",placeholder="Password" }),但没有应用 css。 我如何做到这一点?

【问题讨论】:

    标签: asp.net asp.net-mvc razor


    【解决方案1】:

    如果你的 MVC 版本足够新,那么

    @Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})
    

    否则

    @Html.PasswordFor(model => model.Password, new {@class="form-control", placeholder="Password"})
    

    【讨论】:

      【解决方案2】:

      你仍然可以使用@Html.TextBoxFor:

      @Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })
      

      【讨论】:

        【解决方案3】:

        我也遇到了同样的问题,已经使用了下面的代码,但我不知道为什么它不起作用

        @Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})
        

        但后来我找到了将type ="password"添加到上面的代码中的解决方案

        那么,代码就是现在

        @Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password", Type="password"}})
        

        更新 我找到了另一种解决方法,就是在 LoginViewModel 中的 Password 属性中添加数据注释

        [DataType(DataType.Password)]
        public String Password { get; set; }
        

        然后在视图页面中输入这段代码

            @Html.EditorFor(model => model.Password, new { htmlAttributes = new {@class="form-control", placeholder="Password"}})
        

        【讨论】:

          【解决方案4】:

          您可以尝试使用html input type

          @Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Password", @type = "password" })
          

          【讨论】:

            【解决方案5】:

            这也行得通-

            @Html.Password("Passwordvalue", null, new {@class="form-control", @id= "Passwordvalue", 
            placeholder = "Password"})
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-05
              • 2011-01-25
              • 2012-03-08
              • 2011-12-19
              相关资源
              最近更新 更多