css - Bootstrap "required"类不适用于当前 MVC web 应用程序

标签 css asp.net-mvc twitter-bootstrap asp.net-mvc-4

我目前正在使用 MVC 网络应用程序。我进去重新设计了创建模板,这样做之后,我失去了用红色星号标记必填字段的能力。我不明白为什么我的代码不再有效。请帮忙。代码如下:

HTML/Razor 语法

<div class="form-group required col-md-4">
     @Html.LabelFor(model => model.Buy1FirstName)
     @Html.EditorFor(model => model.Buy1FirstName, new { htmlAttributes = new { @class = "form-control", required = "required" } })
     @Html.ValidationMessageFor(model => model.Buy1FirstName, "Please enter a valid first name", new { @class = "text-danger" })
</div>

我的 CSS/样式表

.form-group .required .control-label:after {
         content:" *" !important;
         color:red !important;
}

这是目前的样子。它应该在标签“名字”之后有一个红色 *。

enter image description here

最佳答案

我认为这里有两个小错误。

  1. 控件标签类引用在您的 css 中,但不在实际的 html 标记中。
  2. 正如评论中提到的那样,css 选择器有点偏离。我刚刚尝试了您的代码,并且能够看到红色星号 这些变化:

    <div class="form-group required col-md-4">
     @Html.LabelFor(model => model.Buy1FirstName, new { @class = "control-label" })
     @Html.EditorFor(model => model.Buy1FirstName, new { htmlAttributes = new { @class = "form-control", required = "required" } })
     @Html.ValidationMessageFor(model => model.Buy1FirstName, "Please enter a valid first name", new { @class = "text-danger" })
    

    .form-group.required .control-label:after {
        content: " *" !important;
        color: red !important;
    }
    

请注意,我所做的唯一更改是将 html 标签中的类引用添加到“control-label”,并删除 css 部分中 .form-group 和 .required 之间的空格。

关于css - Bootstrap "required"类不适用于当前 MVC web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45988696/

相关文章:

firefox - -moz-列跨度 : all; – completely ignored?

asp.net-mvc - 使用 Moq 模拟 HttpContextBase

javascript - 如何将 Javascript 字典传递给 Controller ​​,其中 C# 字典值是一个对象

grails - 使用字段插件修改Grails脚手架模板时发生异常

css - bootstrap 3.3.6 plus图标有变化吗?

css - 带有 Bootstrap 表响应的 DataTables 总是有小的 x 溢出

html - css中的滚动条拇指高度

jquery - CSS/JQuery AddRemove 类需要点击 2 次?

javascript - 如何建立雅虎风格的导航栏?

asp.net - 您是否计划从 ASP.Net Web Forms 迁移到 ASP.Net MVC?