I want to define a class to add an asterisk for required fields, but I know the project I inherited uses Razor mvc, and it doesn't have a head section to put this at the top of my cshtml file.
This is what the start of my _RegistrationStep1.cshtml form looks like:
@{ 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>
...
I haven't found anything in my online search for cshtml files without head section, but CSS definition does say the head section is where to put it if I don't want to define it inline everywhere.
I did see info on how to make the asterisk for required fields dynamic, but I think it's fine for me to just define a second class to add the asterisk.
There's no head in ApplicantCapturedData.cs or Index.cshtml. I thought maybe it would be included somehow. A search of the project for head shows a tag in _Layout.cshtml which is in the application/views/shared folder, but when I added the required class info there, running the project did not show the asterisk on the required field. This is _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>
This is how I used the required class in _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>
...
There is a reference to _Layout.cshtml in _ViewStart.cshtml, which is in application/views folder:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Any help with determining why the required class does not show the asterisk would be appreciated. Thanks!
I see you have a /Content/Site.css
in your Layout. There you can write your css code and will be applied in all your .cshtml file with desired output. So add this code to the Site.css:
label .required:after {
color: #e32;
content: ' *';
display:inline;
}
This a sample of the asterisk but there to many other samples online that can suit you better. For further info see Mdn website