ASP.Net 核心条件 Class 标签助手
ASP.Net Core Conditional Class Tag Helper
在 ASP.Net Core 2.0 中为条件 class 标记助手实现 的解决方案时,我没有得到预期的结果。
标签助手是发布答案的直接副本,未被修改。
按照示例而不是 class 名称,我得到了完整的属性名称以及指定的 class。
我做错了什么?
标签助手
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PerformanceTools.TagHelpers
{
[HtmlTargetElement("div", Attributes = ClassPrefix + "*")]
public class ConditionClassTagHelper : TagHelper
{
private const string ClassPrefix = "condition-class-";
[HtmlAttributeName("class")]
public string CssClass { get; set; }
private IDictionary<string, bool> _classValues;
[HtmlAttributeName("", DictionaryAttributePrefix = ClassPrefix)]
public IDictionary<string, bool> ClassValues
{
get
{
return _classValues ?? (_classValues = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase));
}
set
{
_classValues = value;
}
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var items = _classValues.Where(e => e.Value).Select(e => e.Key).ToList();
if (!string.IsNullOrEmpty(CssClass))
{
items.Insert(0, CssClass);
}
if (items.Any())
{
var classes = string.Join(" ", items.ToArray());
output.Attributes.Add("class", classes);
}
}
}
}
控制器
public IActionResult ConditionalTest()
{
List<ConditionalTestViewModel> model = new List<ConditionalTestViewModel>();
model.Add(new ConditionalTestViewModel { Active = true });
model.Add(new ConditionalTestViewModel { Active = false });
return View("ConditionalTest", model);
}
型号
public class ConditionalTestViewModel
{
public bool Active { get; set; }
}
查看
@model IEnumerable<ConditionalTestViewModel>
<div>Conditional Test</div>
@foreach(var c in Model)
{
<div condition-class-active="@c.Active">@c.Active</div>
}
输出
<div id="container-body" class="container">
<div>Conditional Test</div>
<div condition-class-active="conditional-class-active">True</div>
<div>False</div>
</div>
更新: 正如指出的那样,TagHelper condition-class-*
不是 conditional-class-*
,我已经用这个更新了我的问题,但问题仍然存在。
根据您提到的,您有一个拼写错误。您应该使用 condition-class-active 而不是 conditional-class-active 如下:
<div>Conditional Test</div>
@foreach(var c in Model)
{
<div condition-class-active="@c.Active">@c.Active</div>
}
在 _ViewImports.cshtml 添加对 taghelper 的引用如下
@addTagHelper "*, WebApplication3"
在 ASP.Net Core 2.0 中为条件 class 标记助手实现
标签助手是发布答案的直接副本,未被修改。
按照示例而不是 class 名称,我得到了完整的属性名称以及指定的 class。
我做错了什么?
标签助手
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PerformanceTools.TagHelpers
{
[HtmlTargetElement("div", Attributes = ClassPrefix + "*")]
public class ConditionClassTagHelper : TagHelper
{
private const string ClassPrefix = "condition-class-";
[HtmlAttributeName("class")]
public string CssClass { get; set; }
private IDictionary<string, bool> _classValues;
[HtmlAttributeName("", DictionaryAttributePrefix = ClassPrefix)]
public IDictionary<string, bool> ClassValues
{
get
{
return _classValues ?? (_classValues = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase));
}
set
{
_classValues = value;
}
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var items = _classValues.Where(e => e.Value).Select(e => e.Key).ToList();
if (!string.IsNullOrEmpty(CssClass))
{
items.Insert(0, CssClass);
}
if (items.Any())
{
var classes = string.Join(" ", items.ToArray());
output.Attributes.Add("class", classes);
}
}
}
}
控制器
public IActionResult ConditionalTest()
{
List<ConditionalTestViewModel> model = new List<ConditionalTestViewModel>();
model.Add(new ConditionalTestViewModel { Active = true });
model.Add(new ConditionalTestViewModel { Active = false });
return View("ConditionalTest", model);
}
型号
public class ConditionalTestViewModel
{
public bool Active { get; set; }
}
查看
@model IEnumerable<ConditionalTestViewModel>
<div>Conditional Test</div>
@foreach(var c in Model)
{
<div condition-class-active="@c.Active">@c.Active</div>
}
输出
<div id="container-body" class="container">
<div>Conditional Test</div>
<div condition-class-active="conditional-class-active">True</div>
<div>False</div>
</div>
更新: 正如指出的那样,TagHelper condition-class-*
不是 conditional-class-*
,我已经用这个更新了我的问题,但问题仍然存在。
根据您提到的
<div>Conditional Test</div>
@foreach(var c in Model)
{
<div condition-class-active="@c.Active">@c.Active</div>
}
在 _ViewImports.cshtml 添加对 taghelper 的引用如下
@addTagHelper "*, WebApplication3"