【发布时间】:2017-04-29 07:07:34
【问题描述】:
我正在尝试测试我正在尝试创建的新标签助手。我偶然发现了新的 .net 核心验证的缺点,并且无法更改类后验证。所以如果我想给我的错误一个红色背景,跨度总是在那里并且不会改变。所以,我决定制作自己的标签助手。问题是我似乎无法让它工作或触发。我什至无法让它达到断点。到目前为止,这是我所拥有的标签助手。
namespace MusicianProject.TagHelpers
{
// You may need to install the Microsoft.AspNetCore.Razor.Runtime package into your project
[HtmlTargetElement("invalid-class", Attributes = "validation-class")]
public class ValidateClassTagHelper : TagHelper
{
public ValidateClassTagHelper(IHtmlGenerator generator)
{
}
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
return base.ProcessAsync(context, output);
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("class", "test");
var attr = context.AllAttributes;
}
}
}
这是我的注册视图中的用法。
<div class="container">
<form asp-controller="Account" asp-action="Register" method="post">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label asp-for="FirstName"></label>
<input class="form-control" type="text" asp-for="FirstName" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName"></label>
<input class="form-control" asp-for="LastName" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input class="form-control" type="text" asp-for="Email" />
<span validation-class="alert alert-danger" invalid-class="test" asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" type="password" id="password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" type="password" id="confirm-password" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<div class="btn-group text-center">
<button class="btn btn-default">Sign up!</button>
<button class="btn btn-danger">Cancel</button>
</div>
</div>
</form>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
是的,我已经在我的 _ViewImports.cshtml 文件中注册了项目程序集名称。
@using MusicianProject
@using MusicianProject.Models
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
@addTagHelper "*, MusicianProject"
现在我不确定某些文件的放置是否重要,但我的 _ViewImports.cshtml 文件位于我的视图文件夹根目录 (src/Views/_ViewImports.cshtml) 中,我的标签助手在根目录中有自己的文件夹(src/TagHelpers/*)。
我遗漏了什么,我该如何纠正?
【问题讨论】:
标签: c# razor asp.net-core asp.net-core-mvc tag-helpers