我想定义一个为所需字段添加星号的类,但是我知道我继承的项目使用Razor mvc,并且没有头部将其放在cshtml文件的顶部。
这是我的_RegistrationStep1.cshtml表单的开头:
@{ Layout = null;}
@model MApplicationData.Models.ApplicantCapturedData
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="site-section">
<h4 class="site-header"><b>Department Contact Info:</b> (REQUIRED in case we have questions about this new applicant)</h4>
<hr />
@Html.HiddenFor(model => model.ID)
</div>
<div class="row">
<div class="col-xs-5">
<label class="control-label col-md-6" for="DeptContactName">Department Contact Name</label>
<div class="col-md-4">
@Html.EditorFor(model => model.DeptContactName, new { htmlAttributes = new { @class = "form-control", @required = "required" } })
@Html.ValidationMessageFor(model => model.DeptContactName, "", new { @class = "text-danger" })
</div>
</div>
...
在没有头部分的cshtml文件的在线搜索中,我没有找到任何内容,但是CSS definition确实说,如果我不想在任何地方都内联定义头部分,则将头部分放在哪里。
>我确实看到了有关如何使必填字段的星号动态化的信息,但是我认为只定义一个second class来添加asterisk就可以了。
ApplicantCapturedData.cs或Index.cshtml中没有头。我以为也许会以某种方式包括在内。搜索项目的head会在_Layout.cshtml中的application / views / shared文件夹中显示一个标签,但是当我在其中添加所需的类信息时,运行该项目并没有在所需字段上显示星号。这是_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title - Provider Form</title>
<link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" type="text/css" />
@* Content-box fixes as per http://docs.telerik.com/kendo-ui/third-party/using-kendo-with-twitter-bootstrap article *@
<link href="@Url.Content("~/Content/box-sizing-fixes.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@*<link href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.common-bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<link href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/jszip.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/kendo.all.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/kendo.aspnetmvc.min.js"></script>*@
<link href="http://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<link href="http://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="http://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="http://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.dataviz.bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="http://kendo.cdn.telerik.com/2017.3.1018/js/jquery.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2017.3.1018/js/kendo.all.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2017.3.1018/js/kendo.aspnetmvc.min.js"></script>
<script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script>
<!--I added this class-->
.required:after
{
content: "*";
font-weight: bold;
color:red;
}
</head>
这是我在_RegistrationStep1.cshtml中使用所需类的方式:
<div class="row">
<div class="col-xs-5">
<label class="control-label col-md-6 required" for="DeptContactName">Department Contact Name</label><!--that is required class used-->
<div class="col-md-4">
@Html.EditorFor(model => model.DeptContactName, new { htmlAttributes = new { @class = "form-control", @required = "required" } })
@Html.ValidationMessageFor(model => model.DeptContactName, "", new { @class = "text-danger" })
</div>
</div>
...
在_ViewStart.cshtml中有对_Layout.cshtml的引用,该引用位于application / views文件夹中:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
任何有助于确定所需类为何不显示星号的帮助将不胜感激。谢谢!
答案 0 :(得分:1)
我看到您的版式中有一个/Content/Site.css
。在那里,您可以编写CSS代码,并将其应用到所有具有所需输出的.cshtml文件中。因此,将此代码添加到Site.css:
label .required:after {
color: #e32;
content: ' *';
display:inline;
}
这是一个星号示例,但在线上还有许多其他示例更适合您。有关更多信息,请参见Mdn website