Код: Выделить всё
здесь я хочу использовать два или более атрибута класса и стиля атрибута в div.
Код: Выделить всё
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Smartstore.Web.TagHelpers.Shared
{
[HtmlTargetElement("*", Attributes = AttributesName)]
[HtmlTargetElement("*", Attributes = ConditionalAttributePrefix + "*")]
public class AttributesTagHelper : TagHelper
{
const string AttributesName = "attrs";
const string ConditionalAttributePrefix = "attr-";
private IDictionary _conditionalAttributes;
///
/// We set the order to -1100 to run this TagHelper before .
/// We had to do this because of the QtyInput EditorTemplate which is rendered multiple times on the cart page and needs a unique id and name
/// which will be passed to the EditorTemplate by htmlAttributes dictionary and will be passed to the AttributesTagHelper.
/// does not override the id and name attributes if they are already set.
///
public override int Order => -1100;
///
/// An instance whose content should be merged with local attributes.
///
[HtmlAttributeName(AttributesName)]
public AttributeDictionary Attributes { get; set; }
///
/// Conditional attribute. Output will be suppressed if condition (first value tuple item) evaluates to false.
/// NOTE: the value of conditional class attribute (sm-class) will be appended to existing class attribute.
///
[HtmlAttributeName("sm-all-conditional-attrs", DictionaryAttributePrefix = ConditionalAttributePrefix)]
public IDictionary ConditionalAttributes
{
get
{
return _conditionalAttributes ??= new Dictionary(StringComparer.OrdinalIgnoreCase);
}
set
{
_conditionalAttributes = value;
}
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (Attributes != null)
{
foreach (var attr in Attributes)
{
if (attr.Key == "class")
{
output.AppendCssClass(attr.Value);
}
else
{
output.MergeAttribute(attr.Key, attr.Value, attr.Key == "value");
}
}
}
if (_conditionalAttributes != null && _conditionalAttributes.Count > 0)
{
foreach (var kvp in _conditionalAttributes)
{
if (kvp.Value.Condition)
{
if (kvp.Key == "class")
{
output.AppendCssClass(kvp.Value.Value);
}
else
{
output.MergeAttribute(kvp.Key, kvp.Value.Value, kvp.Key == "value");
}
}
}
}
}
}
}
Может ли HTML-элемент иметь один и тот же атрибут дважды? (3 ответа) Это не имеет никакого отношения к данной теме.
Подробнее здесь: https://stackoverflow.com/questions/790 ... tag-helper