【问题标题】:CSS styling on password input in ASP.NET not displayingASP.NET 中密码输入的 CSS 样式不显示
【发布时间】:2018-03-26 05:18:08
【问题描述】:

我正在制作登录表单,但 CSS 无法在密码输入控件和提交按钮上运行时遇到问题。我正在使用 Bootstrap 3.0,我的代码如下。所有其他表单控件都以正确的样式显示。

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label" })
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label" })
            @Html.PasswordFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", placeholder = "Password" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })               
        </div>

        <div class="form-group">
            <input type="submit" value="Login" class="btn btn-primary" />
        </div>
    </div>
}

这是其显示方式的图像:

Login Form

密码输入应该和用户名输入一样,按钮的宽度应该和上面的输入一样。

【问题讨论】:

    标签: css asp.net asp.net-mvc twitter-bootstrap twitter-bootstrap-3


    【解决方案1】:

    您错误地使用了辅助方法重载。使用您当前的代码,razor 将呈现这样的标记

    <input htmlattributes="{ class = form-control, placeholder = Password }"
                                              id="Password" name="Password" type="password">
    

    这显然是错误的!

    重载的第二个参数需要一个包含 html 属性的对象。但是您传入的对象在 htmlAttributes 属性中有另一个对象。

    解决方法是使用传递正确的对象。这应该工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 2014-12-28
      • 1970-01-01
      • 2019-03-23
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      相关资源
      最近更新 更多