Sho*_*hoe 5 .net c# asp.net asp.net-core asp.net-core-2.0
我添加了以下标记帮助器:
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace X.TagHelpers
{
[HtmlTargetElement(Attributes = ValidationForAttributeName + "," + ValidationErrorClassName)]
public class ValidationClassTagHelper : TagHelper
{
private const string ValidationForAttributeName = "k-validation-for";
private const string ValidationErrorClassName = "k-error-class";
[HtmlAttributeName(ValidationForAttributeName)]
public ModelExpression For { get; set; }
[HtmlAttributeName(ValidationErrorClassName)]
public string ValidationErrorClass { get; set; }
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
Console.WriteLine("\n\n------------!!!!!!---------\n\n");
ModelStateEntry entry;
ViewContext.ViewData.ModelState.TryGetValue(For.Name, out entry);
if (entry == null || !entry.Errors.Any()) return;
var tagBuilder = new TagBuilder(context.TagName);
tagBuilder.AddCssClass(ValidationErrorClass);
output.MergeAttributes(tagBuilder);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在_ViewImports.cshtml我添加了这一行:
@addTagHelper *, X.TagHelpers
Run Code Online (Sandbox Code Playgroud)
该文件已正确编译,如果我引入语法错误dotnet build警告我.
然后在我的一个页面中添加:
<div k-validation-for="OldPassword" k-error-class="has-danger"></div>
Run Code Online (Sandbox Code Playgroud)
如果我加载网页我看到在服务器端没有控制台输出和k-validation-for与k-error-class被转发到为是(相对于加入生成的页has-danger类的class属性).
我究竟做错了什么?
Kir*_*kin 11
注册Tag Helpers时,它是必需的程序集,而不是命名空间 - 在文档中进行了解释.
...第二个参数"Microsoft.AspNetCore.Mvc.TagHelpers"指定包含Tag Helpers的程序集.Microsoft.AspNetCore.Mvc.TagHelpers是内置ASP.NET核心标记助手的程序集.
所以在你的情况下,你可以改变这个:
@addTagHelper *, X.TagHelpers
Run Code Online (Sandbox Code Playgroud)
对此:
@addTagHelper *, X
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1499 次 |
| 最近记录: |