MVC验证仅边框突出显示没有文本错误消息

时间:2016-01-30 07:30:14

标签: css validation asp.net-mvc-3 model-view-controller twitter-bootstrap-3

您好我只想突出显示文本框的边框,附近没有任何错误消息,我尝试了以下内容,但它仍然添加了span标记,我可以避免添加span标记吗?谢谢!

    [Required(ErrorMessage = " ")]
    [Display(Name = "Email")]
    public string Email { get; set; }

视图

        @using (Html.BeginForm("Login", "Account", new {
            ReturnUrl = ViewBag.ReturnUrl
        }, FormMethod.Post, new {
            @class = "navbar-form navbar-nav",
            id = "userForm"
        })) {
            @Html.AntiForgeryToken()

            <div class="form-group">
                @Html.ValidationMessageFor(m => m.Email, "", new {
                    @class = "text-danger"
                })
                @Html.TextBoxFor(m => m.Email, new {
                    @class = "form-control input-sm",
                    @placeholder = "Email"
                })
            </div>
            <div class="form-group">
                @Html.ValidationMessageFor(m => m.Password, "", new {
                    @class = "text-danger"
                }) @Html.PasswordFor(m => m.Password, new {
                       @class = "form-control input-sm",
                       @placeholder = "Password"
                   })
            </div>
            <input type="submit" value="Log in" class="btn btn-sm btnMain"/>
        }

1

2

编辑:添加了视图。

2 个答案:

答案 0 :(得分:0)

使用类装饰您的验证摘要,并在CSS中为其添加样式display:none;在视图中

@Html.ValidationSummary("", new { @class = "text-danger hidevalidation" })
CSS中的

.text-danger .hidevalidation{
    display:none;
}

答案 1 :(得分:0)

对我有用的是在ValidationMessageFor中添加另一个类,以便仅在某些位置禁用文本消息。

新观点:

   @using (Html.BeginForm("Login", "Account", new {
        ReturnUrl = ViewBag.ReturnUrl
    }, FormMethod.Post, new {
        @class = "navbar-form navbar-nav",
        id = "userForm"
    })) {
        @Html.AntiForgeryToken()

        <div class="form-group">
            @Html.ValidationMessageFor(m => m.Email, "", new {
                @class = "text-danger nav-text-danger"
            })
            @Html.TextBoxFor(m => m.Email, new {
                @class = "form-control input-sm",
                @placeholder = "Email"
            })
        </div>
        <div class="form-group">
            @Html.ValidationMessageFor(m => m.Password, "", new {
                @class = "text-danger nav-text-danger"
            }) @Html.PasswordFor(m => m.Password, new {
                   @class = "form-control input-sm",
                   @placeholder = "Password"
               })
        </div>
        <input type="submit" value="Log in" class="btn btn-sm btnMain"/>
    }

他们新添加了课程:

.nav-text-danger { display: none; }

为了添加红色边框,我添加/修改了以下CSS类:

.input-validation-error { border: 2px solid #ff0000 !important; }

特别感谢@stephen-muecke帮助我解决这个问题