/// <summary>
/// 文本域标签
/// </summary>
[HtmlTargetElement("layuiTextareaTag", TagStructure = TagStructure.WithoutEndTag)]
public class LayuiTextareaTag:TagHelper
{
#region 标签属性
private const string TextAttribute = "text";
private const string ForAttribute = "asp-for";
private const string NameAttribute = "name";
private const string DisabledAttribute = "disabled";
#endregion
#region 实体属性
[HtmlAttributeName(TextAttribute)]
public string Text { get; set; }
[HtmlAttributeName(ForAttribute)]
public ModelExpression For { get; set; }
[HtmlAttributeName(NameAttribute)]
public string Name { get; set; }
[HtmlAttributeName(DisabledAttribute)]
public bool Disabled { get; set; }
#endregion
#region 标签输出
public override void Process(TagHelperContext context, TagHelperOutput output)
{
string desciption = For.ModelExplorer.Metadata.Description ?? For.ModelExplorer.Metadata.DisplayName;
var value = For.ModelExplorer.Model == null ? "" : For.ModelExplorer.Model.ToString();
var htmlLabel = $"<label class='layui-form-label'>{(string.IsNullOrEmpty(desciption)?"文本域":desciption)}</label>";
var htmlArea = $@"<div class='layui-input-block'>
<textarea name='{For.Name}' placeholder='请输入内容' class='layui-textarea'>{value}</textarea>
</div>";
output.TagName = "div";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.Add("id",$"{For.Name.ToLower()}_div");
output.Attributes.Add("class","layui-form-item");
output.Content.SetHtmlContent(htmlLabel + "\n"+htmlArea);
}
#endregion
}
利用TagHelper实现简单的自定义标签,一个标签对应一个类。
标签属性用于html页面,实体属性可以接收标签属性的值。
重写Process方法,以上是个人的简单理解。