如何将Bootstrap ValidationMessageFor置于正确的位置?

时间:2014-08-04 06:26:15

标签: css twitter-bootstrap validation message

我正在为我的Web应用程序使用bootstrap,并且我在验证消息时遇到问题,因为它将消息状态置于正确的位置。现在我使用此link中的此示例,使用以下代码:

<div class="control-group error">
  <label class="control-label" for="inputError">Input with error</label>
  <div class="controls">
    <input type="text" id="inputError">
    <span class="help-inline">Please correct the error</span>
  </div>
</div>

现在我使用的代码:

<h4 class="form-signin-heading">Please sign in</h4>
<div class="control-group">
    <div class="controls">                 
        @Html.TextBoxFor(u => u.Username, new {placeholder="Username", @class="form-control username", id="inputError"}) 
        <span class="help-inner">@Html.ValidationMessageFor(u => u.Username)</span>                        
    </div>

    <div class="controls">       
        @Html.PasswordFor(u => u.Password, new {placeholder="Password", @class="form-control password"}) 
        @Html.ValidationMessageFor(u=> u.Password)
    </div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

我的代码执行此操作(也在小提琴中:http://jsfiddle.net/4h5E5/1/):

enter image description here

但我的不满是将验证信息放在这个位置(如链接中的示例):

enter image description here

3 个答案:

答案 0 :(得分:5)

请检查此JS Fiddle Link

  

HTML代码:

<!DOCTYPE html>
<body>
    <div class="container">
        <form class="form-signin form-horizontal" role="form">
             <h4 class="form-signin-heading">Please sign in</h4>

            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <input type="username" class="form-control username" placeholder="Username" required="autofocus" />
                </div>
                <div class="col-xs-7 col-sm-5"> <span class="help-inline pull-left">Please correct the error</span>

                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <input type="password" class="form-control password" placeholder="Password" />
                </div>
                <div class="col-xs-7 col-sm-5"> <span class="help-inline pull-left">Please correct the error</span>

                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                </div>
            </div>
        </form>
    </div>
    <!-- /container -->
    <!-- Bootstrap core JavaScript==================================================- ->
    <!-- Placed at the end of the document so the pages load faster -->
</body>

我希望这是你想要实现的目标。

如果您有任何其他问题或未解决,请在下面添加评论。

问候D.

答案 1 :(得分:1)

你试过这个吗? (另)

  <div class="controls">
    <input type="text" id="inputError" class="pull-left">
    <span class="help-inline pull-left">Please correct the error</span>
  </div>
如果你做一个小提琴,那将会很棒。

答案 2 :(得分:0)

style = "display:inline"EditorFor使用ValidationMessageFor,如下所示:

<div class="form-group">
    @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2"})

    <div class="col-md-9">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", style = "display:inline" } })
        @Html.ValidationMessageFor(model => model.UserName, "*", new { @class = "text-danger", style = "display:inline" })
    </div>

</div>