MVC 验证只有边框高亮没有文本错误信息
MVC validation only border highlight without text error message
您好,我想只高亮显示文本框的边框,附近没有任何错误消息,我已经尝试了以下方法,但它仍然添加了 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
编辑:添加了视图。
用 class 装饰您的验证摘要并在 CSS 中添加样式 display:none;
:
在视图中:
@Html.ValidationSummary("", new { @class = "text-danger hidevalidation" })
在CSS:
.text-danger .hidevalidation{
display:none;
}
对我有用的是向 ValidationMessageFor 添加另一个 class,以便仅在某些位置禁用文本消息。
新视图:
@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"/>
}
他们新添加了class:
.nav-text-danger { display: none; }
为了添加红色边框,我有 added/modified 以下 CSS class:
.input-validation-error { border: 2px solid #ff0000 !important; }
特别感谢 @stephen-muecke 帮助我完成这个
您好,我想只高亮显示文本框的边框,附近没有任何错误消息,我已经尝试了以下方法,但它仍然添加了 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
编辑:添加了视图。
用 class 装饰您的验证摘要并在 CSS 中添加样式 display:none;
:
在视图中:
@Html.ValidationSummary("", new { @class = "text-danger hidevalidation" })
在CSS:
.text-danger .hidevalidation{
display:none;
}
对我有用的是向 ValidationMessageFor 添加另一个 class,以便仅在某些位置禁用文本消息。
新视图:
@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"/>
}
他们新添加了class:
.nav-text-danger { display: none; }
为了添加红色边框,我有 added/modified 以下 CSS class:
.input-validation-error { border: 2px solid #ff0000 !important; }
特别感谢 @stephen-muecke 帮助我完成这个