我需要为必填字段自动显示星号,因此我设法找到了一些在线代码。我添加了一个名为"必需标签"的CSS类。也把它变成红色。但是,它只将CSS类应用于星号而不是标签。任何想法如何将CSS类应用于两者?这是所要求的完整代码段。
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Threading.Tasks;
namespace App.TagHelpers
{
[HtmlTargetElement("label", Attributes = ForAttributeName)]
public class LabelRequiredTagHelper : LabelTagHelper
{
private const string ForAttributeName = "asp-for";
public LabelRequiredTagHelper(IHtmlGenerator generator) : base(generator)
{
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
await base.ProcessAsync(context, output);
if (For.Metadata.IsRequired)
{
var sup = new TagBuilder("sup");
sup.InnerHtml.Append("*");
sup.AddCssClass("required-label");
output.Content.AppendHtml(sup);
}
}
}
}

感谢Manoj Kulkarni的代码示例。
答案 0 :(得分:4)
OP中的标记帮助程序可以工作,但我认为你需要做的就是向标签元素本身添加一个css类,并相应地使用CSS来设置标签的样式,而不是附加一个包含星号的sup元素。
tag-helper
[HtmlTargetElement("label", Attributes = ForAttributeName)]
public class LabelRequiredTagHelper : LabelTagHelper
{
private const string ForAttributeName = "asp-for";
private const string RequiredCssClass = "required";
public LabelRequiredTagHelper(IHtmlGenerator generator) : base(generator)
{
}
public override async Task ProcessAsync(TagHelperContext context,
TagHelperOutput output)
{
await base.ProcessAsync(context, output);
if (For.Metadata.IsRequired)
{
output.Attributes.AddCssClass(RequiredCssClass);
}
}
}
AddCssClass
扩展名public static class TagHelperAttributeListExtensions
{
public static void AddCssClass(this TagHelperAttributeList attributeList,
string cssClass)
{
var existingCssClassValue = attributeList
.FirstOrDefault(x => x.Name == "class")?.Value.ToString();
// If the class attribute doesn't exist, or the class attribute
// value is empty, just add the CSS class
if (String.IsNullOrEmpty(existingCssClassValue))
{
attributeList.SetAttribute("class", cssClass);
}
// Here I use Regular Expression to check if the existing css class
// value has the css class already. If yes, you don't need to add
// that css class again. Otherwise you just add the css class along
// with the existing value.
// \b indicates a word boundary, as you only want to check if
// the css class exists as a whole word.
else if (!Regex.IsMatch(existingCssClassValue, $@"\b{ cssClass }\b",
RegexOptions.IgnoreCase))
{
attributeList.SetAttribute("class", $"{ cssClass } { existingCssClassValue }");
}
}
}
包含必需属性的示例视图模型,使用[Required]
注释。默认情况下也需要布尔值。
public class LoginViewModel
{
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember my login?")]
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}
例如,我有一个需要LoginViewModel
的登录页面。
@model LoginViewModel
@{
ViewData["Title"] = "Login";
}
<form asp-area="" asp-controller="account" asp-action="login">
<input type="hidden" asp-for="ReturnUrl" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email"></label>
<input type="email" class="form-control" asp-for="Email" />
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input type="password" class="form-control" asp-for="Password" />
</div>
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" asp-for="RememberMe" />
<label asp-for="RememberMe" class="custom-control-label"></label>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
值得知道复选框RememberMe
的标签。在视图中,我添加了额外的css类custom-control-label
,标记帮助程序仍然设法添加所需的css类required
。
你可以告诉我正在使用Bootstrap css框架,并且已经有复选框标签的样式,所以我想要排除那些(用custom-control-label
css类表示)。
label.required:not(.custom-control-label)::after {
content: "*";
padding-left: .3rem;
color: theme-color('danger'); /* color: #dc3545; */
}
如果您希望所需的标签也是红色,您可以这样设置样式:
label.required:not(.custom-control-label) {
color: theme-color('danger'); /* color: #dc3545; */
&::after {
content: "*";
padding-left: .3rem;
}
}